如何在 Java GridLayout 中高效取得按鈕的 X 和 Y 索引?
如何輕鬆取得 GridLayout 中按鈕的 X 和 Y 索引
在 Java 中使用 GridLayout 時,您可能會遇到需要確定 x 和 Y 索引的情況。網格內特定按鈕的 y 索引。傳統方法涉及巢狀循環,迭代二維按鈕陣列以找到您要尋找的按鈕。
<br> @Override<br> public void actionPerformed(ActionEvent ae) {<pre class="brush:php;toolbar:false">JButton bx = (JButton) ae.getSource(); for (int i = 0; i < 5; i++) for (int j = 0; j < 5; j++) if (b[i][j] == bx) { bx.setBackground(Color.RED); }
}
}
}
<🎜方法可能很乏味且容易出錯。幸運的是,有一種更有效的方法來檢索按鈕的座標。
引入GridButtonPanel 類別
以下Java 程式碼展示了一個簡化的解決方案:
</p>導入java.awt.<ul><li> <pre class="brush:php;toolbar:false"><br>導入java.awt.</li></ul> <pre class="brush:php;toolbar:false"><p>導入java.awt.</p><pre class="brush:php;toolbar:false">private static final int N = 5; private final List<JButton> list = new ArrayList<JButton>(); private JButton getGridButton(int r, int c) { int index = r * N + c; return list.get(index); } private JButton createGridButton(final int row, final int col) { final JButton b = new JButton("r" + row + ",c" + col); b.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JButton gb = GridButtonPanel.this.getGridButton(row, col); System.out.println("r" + row + ",c" + col + " " + (b == gb) + " " + (b.equals(gb))); } }); return b; } private JPanel createGridPanel() { JPanel p = new JPanel(new GridLayout(N, N)); for (int i = 0; i < N * N; i++) { int row = i / N; int col = i % N; JButton gb = createGridButton(row, col); list.add(gb); p.add(gb); } return p; } private void display() { JFrame f = new JFrame("GridButton"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(createGridPanel()); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { new GridButtonPanel().display(); } }); }
<p>導入java.awt. ;<br>導入java.awt.GridLayout;</p>導入java.awt.event.ActionEvent;<p>導入java.awt.event.ActionListener;</p>導入java.util.ArrayList;<p>導入java.util.List; <strong>導入javax.swing.JButton; </strong>導入javax.swing.JFrame;</p>導入javax.swing.JPanel;<p></p>/**<p><strong></strong>@請參閱http://stackoverflow.com/questions/7702697</p>*/<p></p><p>公共類別 GridButtonPanel {<strong></strong></p>}<p>前></p><p>在此代碼:</p><p><strong>1。 getGridButton() 方法</strong>:</p><p>使用此方法,您可以根據按鈕的 x 和 y 座標從網格中擷取按鈕。為了確定清單中的索引,它將行數 (N) 乘以指定的行 (r),並新增指定的欄位 (c)。 <strong></strong></p>2。動作監聽器<pre class="brush:php;toolbar:false">r2,c3 true true
以上是如何在 Java GridLayout 中高效取得按鈕的 X 和 Y 索引?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

公司安全軟件導致部分應用無法正常運行的排查與解決方法許多公司為了保障內部網絡安全,會部署安全軟件。 ...

系統對接中的字段映射處理在進行系統對接時,常常會遇到一個棘手的問題:如何將A系統的接口字段有效地映�...

在使用MyBatis-Plus或其他ORM框架進行數據庫操作時,經常需要根據實體類的屬性名構造查詢條件。如果每次都手動...

將姓名轉換為數字以實現排序的解決方案在許多應用場景中,用戶可能需要在群組中進行排序,尤其是在一個用...

在使用IntelliJIDEAUltimate版本啟動Spring...

Java對象與數組的轉換:深入探討強制類型轉換的風險與正確方法很多Java初學者會遇到將一個對象轉換成數組的�...

電商平台SKU和SPU表設計詳解本文將探討電商平台中SKU和SPU的數據庫設計問題,特別是如何處理用戶自定義銷售屬...

在使用TKMyBatis進行數據庫查詢時,如何優雅地獲取實體類變量名以構建查詢條件,是一個常見的難題。本文將針...
