我正在制作一个井字游戏,我希望能够在单击时让按钮交替 x 和 o。现在它们在第一次点击时都是 x ,在第二次点击时都是 o 。我也尝试过使用和不使用关键字 this 。这是按钮类public class Toebuttons extends JButton implements ActionListener{boolean x = true; // if true x's turn if false o's turnint count = 0;public Toebuttons(){ super("blank"); this.addActionListener(this);}public void actionPerformed(ActionEvent e){ if(this.x == true) { count++; System.out.println(count); setText("X"); this.x = false; } else if(this.x == false) { count++; System.out.println(count); setText("O"); this.x = true; } }}这是板类public class ticTacBoard extends JFrame{Toebuttons toe[] = new Toebuttons[9];public ticTacBoard(){ super("Tic tac board"); setSize(500,500); setLayout(new GridLayout(3,3)); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); toFront(); for(int i = 0; i<toe.length; i++) { toe[i] = new Toebuttons(); add(toe[i]); } setVisible(true); }}
2 回答
UYOU
TA贡献1878条经验 获得超4个赞
这不是 C++ 而是这个
boolean x = true;
不是global
Java 中的 a。到可以在Java中被理解为“全球”(通用于所有的类实例)模拟变量需要声明它static
像
static boolean x = true;
添加回答
举报
0/150
提交
取消