我的actionPerformed方法有问题。我想在每次按下按钮时更改按钮图像。如果我为一个按钮(例如button[0][0]
)执行此操作,它可以正常工作,但是对于所有我来说
java.lang.ArrayIndexOutOfBoundsException
我被困在这里,我不知道如何解决这个问题...
public class ExButtons {
JButton[][] button = new JButton[4][5];
ImageIcon[][] img = new ImageIcon[4][5];
ImageIcon cardTurned = new ImageIcon();
private final JPanel panel = new JPanel();
private final JFrame frame = new JFrame();
private int i, j;
public ExButtons() {
img[0][0] = new ImageIcon("...");
img[0][1] = new ImageIcon("...");
img[0][2] = new ImageIcon("...");
img[0][3] = new ImageIcon("...");
img[0][4] = new ImageIcon("...");
img[1][0] = new ImageIcon("...");
img[1][1] = new ImageIcon("...");
img[1][2] = new ImageIcon("...");
img[1][3] = new ImageIcon("...");
img[1][4] = new ImageIcon("...");
img[2][0] = new ImageIcon("...");
img[2][1] = new ImageIcon("...");
img[2][2] = new ImageIcon("...");
img[2][3] = new ImageIcon("...");
img[2][4] = new ImageIcon("...");
img[3][0] = new ImageIcon("...");
img[3][1] = new ImageIcon("...");
img[3][2] = new ImageIcon("...");
img[3][3] = new ImageIcon("...");
img[3][4] = new ImageIcon("...");
cardTurned = new ImageIcon("...");
for (i = 0; i < button.length; i++) {
for (j = 0; j < button[0].length; j++) {
button[i][j] = new JButton(img[i][j]);
}
}
int x = 100, y = 100;
for (i = 0; i < button.length; i++) {
for (j = 0; j < button[0].length; j++) {
button[i][j].setBounds(x, y, 60, 60);
panel.add(button[i][j]);
x += 80;
if (j == 4) {
x = 100;
y += 80;
}
}
}
for (i = 0; i < button.length; i++) {
for (j = 0; j < button[0].length; j++) {
button[i][j].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Button[" + i + "][" + j + "] was pressed");
button[i][j].setIcon(button[i][j].getIcon() == img[i][j] ? cardTurned : img[i][j]);
}
});
}
}
frame.setSize(600, 600);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.setLayout(null);
for (i = 0; i < button.length; i++) {
for (j = 0; j < button[0].length; j++) {
panel.add(button[i][j]);
}
}
frame.add(panel);
frame.setVisible(true);
}
public static void main(String[] args) {
new ExButtons().frame.setVisible(true);
}
}
答案 0 :(得分:1)
在循环结束时,i和j都超出范围。因此,所有听众都使用i和j的这些超出范围的值。
我和j不应该是字段。它们应该是局部变量。由于听众需要最终变量,因此需要这些变量的最终副本:
for (int i = 0; i < button.length; i++) {
for (int j = 0; j < button[0].length; j++) {
final int i2 = i;
final int j2 = j;
button[i][j].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Button[" + i2 + "][" + j2 + "] was pressed");
button[i2][j2].setIcon(button[i2][j2].getIcon() == img[i2][j2] ? cardTurned : img[i2][j2]);
}
});
}
}
或者,更简单:
for (int i = 0; i < button.length; i++) {
for (int j = 0; j < button[0].length; j++) {
final JButton btn = button[i][j];
final ImageIcon image = img[i][j];
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
btn.setIcon(btn.getIcon() == image ? cardTurned : image);
}
});
}
}
你也应该放弃设置按钮边界的想法。请改用布局管理器。这是它的工作。