Răz*_*nda 15 java swing awt grid-layout
我正在研究一个java教程,看到在GridLayout中找到JButton的x/y索引的方法是遍历与布局关联的按钮b的二维数组,并检查是否
b[i][j] == buttonReference.
@Override
public void actionPerformed(ActionEvent ae) {
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);
}
}
Run Code Online (Sandbox Code Playgroud)
是否有更简单的方法来获取按钮的X/Y索引?
就像是:
JButton button = (JButton) ev.getSource();
int x = this.getContentPane().getComponentXIndex(button);
int y = this.getContentPane().getComponentYIndex(button);
Run Code Online (Sandbox Code Playgroud)
this是一个GameWindow实例,ev当用户按下按钮时触发ActionEvent.

在这种情况下,它应该得到:x == 2,y == 1
@ GameWindow.java:
package javaswingapplication;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.*;
public class GameWindow extends JFrame implements ActionListener
{
JButton b[][] = new JButton[5][5];
int v1[] = { 2, 5, 3, 7, 10 };
int v2[] = { 3, 5, 6, 9, 12 };
public GameWindow(String title)
{
super(title);
setLayout(new GridLayout(5, 5));
setDefaultCloseOperation(EXIT_ON_CLOSE );
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
{
b[i][j] = new JButton();
b[i][j].addActionListener(this);
add(b[i][j]);
}
}
@Override
public void actionPerformed(ActionEvent ae) {
((JButton)ae.getSource()).setBackground(Color.red);
}
}
Run Code Online (Sandbox Code Playgroud)
@ JavaSwingApplication.java:
package javaswingapplication;
public class JavaSwingApplication {
public static void main(String[] args) {
GameWindow g = new GameWindow("Game");
g.setVisible(true);
g.setSize(500, 500);
}
}
Run Code Online (Sandbox Code Playgroud)
tra*_*god 18
此示例显示如何创建一个知道其在网格上的位置的网格按钮.该方法getGridButton()显示如何基于其网格坐标有效地获得按钮引用,并且动作侦听器显示单击和找到的按钮是相同的.

package gui;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* @see http://stackoverflow.com/questions/7702697
*/
public class GridButtonPanel {
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();
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
你已经保存了所有JButton的数组; 你可以搜索ae.getSource(),你有位置.
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if( b[i][j] == ae.getSource() ) {
// position i,j
}
}
}
Run Code Online (Sandbox Code Playgroud)
来自JButtons
的JButton#的setName(字符串);
的JButton#setActionCommand(字符串);
JButton的#的setAction(行动);
从/到集装箱
SwingUtilities类#getDeepestComponentAt