我正在制作一个井字游戏,我希望能够在单击时让按钮交替 x 和 o。现在它们在第一次点击时都是 x ,在第二次点击时都是 o 。我也尝试过使用和不使用关键字 this 。
这是按钮类
public class Toebuttons extends JButton implements ActionListener
{
boolean x = true; // if true x's turn if false o's turn
int 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);
}
}
UYOU
相关分类