【发布时间】:2020-02-21 20:04:38
【问题描述】:
我想制作一个井字游戏,但我遇到了问题。
我制作了一个二维数组,但我不知道如何解决它们以使 ActionListener 正常工作。
这是我的代码:
public class GUI {
public static JButton[][] buttonsall = new JButton[3][3];
public static JFrame frame = new JFrame("TIC TAC TOE");
public static void draw() {
// frame
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(false);
frame.setBackground(Color.white);
frame.setBounds(500, 500, 600,600);
// actionListener
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof JButton) {
//dont know further here
}
}
};
//buttons buttonsall[x][y]
for (int y = 0; y<3;y++) {
for (int x = 0; x<3;x++) {
buttonsall[x][y] = new JButton();
buttonsall[x][y].setVisible(true);
buttonsall[x][y].setSize(80, 80);
buttonsall[x][y].addActionListener(listener);
System.out.println(buttonsall[x][y] +" "+x +" "+y);
frame.add(buttonsall[x][y]);
buttonsall[x][y].setBackground(Color.white);
}
}
frame.setLayout(new GridLayout(3,3));
}
【问题讨论】: