我在游戏包中创建了两个类,第一类 gui 用于 gui,其他类 main 用于主要编码,我遇到的问题是,当我单击任何按钮按钮将文本设置为“x”时,我无法切换到其他玩家请帮助我,我真的很困惑,谢谢大家。
package game;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class gui implements ActionListener
{
main main=new main();
JButton b1,b2,b3,b4,b5,b6,b7,b8,b9;
JLabel l;
public gui()
{
JFrame f=new JFrame("TicTacToe");
JPanel p=new JPanel(new FlowLayout());
JPanel p1=new JPanel(new GridLayout(3,3));
JPanel p2=new JPanel(new GridLayout(2,1));
f.setSize(400,600);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(p);
f.add(p1);
f.add(p2);
p2.add(p);
p2.add(p1);
l=new JLabel();
b1=new JButton();
b2=new JButton();
b3=new JButton();
b4=new JButton();
b5=new JButton();
b6=new JButton();
b7=new JButton();
b8=new JButton();
b9=new JButton();
p.add(l);
p1.add(b1);
p1.add(b2);
p1.add(b3);
p1.add(b4);
p1.add(b5);
p1.add(b6);
p1.add(b7);
p1.add(b8);
p1.add(b9);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
}
public static void main (String args[])
{
gui i=new gui();
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b1)
{
main.chkTurn();
if(main.playerTurn=true)
{
b1.setText("X");
main.checkWin();
main.playerTurn=false;
}
if(main.playerTurn=false)
{
b1.setText("O");
main.checkWin();
main.playerTurn=true;
}
}
if(ae.getSource()==b2)
{
main.chkTurn();
if(main.playerTurn=true)
{
b2.setText("X");
main.checkWin();
main.playerTurn=false;
}
if(main.playerTurn=false)
{
b2.setText("O");
main.checkWin();
main.playerTurn=true;
}
}
if(ae.getSource()==b3)
{
main.chkTurn();
if(main.playerTurn=true)
{
b3.setText("X");
main.checkWin();
main.playerTurn=false;
}
if(main.playerTurn=false)
{
b3.setText("O");
main.checkWin();
main.playerTurn=true;
}
}
if(ae.getSource()==b4)
{
main.chkTurn();
if(main.playerTurn=true)
{
b4.setText("X");
main.checkWin();
main.playerTurn=false;
}
if(main.playerTurn=false)
{
b4.setText("O");
main.checkWin();
main.playerTurn=true;
}
}
if(ae.getSource()==b5)
{
main.chkTurn();
if(main.playerTurn=true)
{
b5.setText("X");
main.checkWin();
main.playerTurn=false;
}
if(main.playerTurn=false)
{
b5.setText("O");
main.checkWin();
main.playerTurn=true;
}
}
if(ae.getSource()==b6)
{
main.chkTurn();
if(main.playerTurn=true)
{
b6.setText("X");
main.checkWin();
main.playerTurn=false;
}
if(main.playerTurn=false)
{
b6.setText("O");
main.checkWin();
main.playerTurn=true;
}
}
if(ae.getSource()==b7)
{
main.chkTurn();
if(main.playerTurn=true)
{
b7.setText("X");
main.checkWin();
main.playerTurn=false;
}
if(main.playerTurn=false)
{
b7.setText("O");
main.checkWin();
main.playerTurn=true;
}
}
if(ae.getSource()==b8)
{
main.chkTurn();
if(main.playerTurn=true)
{
b8.setText("X");
main.checkWin();
main.playerTurn=false;
}
if(main.playerTurn=false)
{
b8.setText("O");
main.checkWin();
main.playerTurn=true;
}
}
if(ae.getSource()==b9)
{
main.chkTurn();
if(main.playerTurn=true)
{
b9.setText("X");
main.checkWin();
main.playerTurn=false;
}
if(main.playerTurn=false)
{
b9.setText("O");
main.checkWin();
main.playerTurn=true;
}
}
}
}
和其他类是
package game;
public class main {
public static boolean playerTurn=true;
public static int event=0;
public static boolean playerWon=false;
public static boolean comWon=false;
public static gui board=new gui();
public static void main(String args[])
{
//main main=new main();
}
public static void chkTurn()
{
if(event==(1|3|5|7|9))
{
playerTurn=true;
}
else if(event==(2|4|6|8|10))
{
playerTurn=false;
}
}
public static void checkWin()
{
if(board.b1.getText().equals("X"))
{
if(board.b4.getText().equals("X"))
{
if(board.b7.getText().equals("X"))
{
playerWon=true;
comWon=false;
board.l.setText("Player 1 Won!");
}
}
}
if(board.b1.getText().equals("X"))
{
if(board.b5.getText().equals("X"))
{
if(board.b9.getText().equals("X"))
{
playerWon=true;
comWon=false;
board.l.setText("Player 1 Won!");
}
}
}
if(board.b1.getText().equals("X"))
{
if(board.b2.getText().equals("X"))
{
if(board.b3.getText().equals("X"))
{
playerWon=true;
comWon=false;
board.l.setText("Player 1 Won!");
}
}
}
if(board.b3.getText().equals("X"))
{
if(board.b5.getText().equals("X"))
{
if(board.b7.getText().equals("X"))
{
playerWon=true;
comWon=false;
board.l.setText("Player 1 Won!");
}
}
}
if(board.b3.getText().equals("X"))
{
if(board.b5.getText().equals("X"))
{
if(board.b9.getText().equals("X"))
{
playerWon=true;
comWon=false;
board.l.setText("Player 1 Won!");
}
}
}
if(board.b7.getText().equals("X"))
{
if(board.b8.getText().equals("X"))
{
if(board.b9.getText().equals("X"))
{
playerWon=true;
comWon=false;
board.l.setText("Player 1 Won!");
}
}
}
if(board.b4.getText().equals("X"))
{
if(board.b5.getText().equals("X"))
{
if(board.b6.getText().equals("X"))
{
playerWon=true;
comWon=false;
board.l.setText("Player 1 Won!");
}
}
}
if(board.b2.getText().equals("X"))
{
if(board.b5.getText().equals("X"))
{
if(board.b8.getText().equals("X"))
{
playerWon=true;
comWon=false;
board.l.setText("Player 1 Won!");
}
}
}
if(board.b1.getText().equals("O"))
{
if(board.b4.getText().equals("O"))
{
if(board.b7.getText().equals("O"))
{
playerWon=false;
comWon=true;
board.l.setText("Player 2 Won!");
}
}
}
if(board.b1.getText().equals("O"))
{
if(board.b5.getText().equals("O"))
{
if(board.b9.getText().equals("O"))
{
playerWon=false;
comWon=true;
board.l.setText("Player 2 Won!");
}
}
}
if(board.b1.getText().equals("O"))
{
if(board.b2.getText().equals("O"))
{
if(board.b3.getText().equals("O"))
{
playerWon=false;
comWon=true;
board.l.setText("Player 2 Won!");
}
}
}
if(board.b3.getText().equals("O"))
{
if(board.b5.getText().equals("O"))
{
if(board.b7.getText().equals("O"))
{
playerWon=false;
comWon=true;
board.l.setText("Player 2 Won!");
}
}
}
if(board.b3.getText().equals("O"))
{
if(board.b5.getText().equals("O"))
{
if(board.b9.getText().equals("O"))
{
playerWon=false;
comWon=true;
board.l.setText("Player 2 Won!");
}
}
}
if(board.b7.getText().equals("O"))
{
if(board.b8.getText().equals("O"))
{
if(board.b9.getText().equals("O"))
{
playerWon=false;
comWon=true;
board.l.setText("Player 2 Won!");
}
}
}
if(board.b4.getText().equals("O"))
{
if(board.b5.getText().equals("O"))
{
if(board.b6.getText().equals("O"))
{
playerWon=false;
comWon=true;
board.l.setText("Player 2 Won!");
}
}
}
if(board.b2.getText().equals("O"))
{
if(board.b5.getText().equals("O"))
{
if(board.b8.getText().equals("O"))
{
playerWon=false;
comWon=true;
board.l.setText("Player 2 Won!");
}
}
}
}
}