使用数组按下按钮时更改 JButton 图像

Changing a JButton image when a button is pressed using an array

我的 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);
}
}

循环结束时,i和j都出界了。因此,您所有的听众都将这些越界值用于 i 和 j。

i 和 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);

            }
        });
    }
}

你也绝对应该放弃设置按钮边界的想法。请改用布局管理器。那是它的工作。