Netbeans 中带有 Java GUI 的 Tic Tac Toe 游戏显示不正确

Incorrect Display with Tic Tac Toe game with Java GUI in Netbeans

我的目标是在两个玩家(没有 computerized/random 玩家)之间创建一个可以重新开始的 TicTacToe 游戏。我有大部分代码,但是当我 运行 代码而不是在文本字段中说“X's turn”或“O's turn”时,它直接进入 'X wins ' 或 'O wins' 甚至'wins'。我将如何修复此错误?

import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class TicTac extends JFrame {

JButton[][] ticTac = new JButton[3][3];
private JButton button1 = ticTac[0][0];
private JButton button2 = ticTac[0][1];
private JButton button3 = ticTac[0][2];
private JButton button4 = ticTac[1][0];
private JButton button5 = ticTac[1][1];
private JButton button6 = ticTac[1][2];
private JButton button7 = ticTac[2][0];
private JButton button8 = ticTac[2][1];
private JButton button9 = ticTac[2][2];
private final JButton reset;
private final JPanel panel;
private final JTextField text;
Font font1 = new Font("Courier New", Font.BOLD, 18);
Font font2 = new Font("Courier New", Font.BOLD, 60);
private int player = 2;

/**
 * Constructor for the TicTac
 */
public TicTac() {
    // Creating GUI
    button1 = new JButton("");
    button2 = new JButton("");
    button3 = new JButton("");
    button4 = new JButton("");
    button5 = new JButton("");
    button6 = new JButton("");
    button7 = new JButton("");
    button8 = new JButton("");
    button9 = new JButton("");
    reset = new JButton("New Game");
    text = new JTextField("");
    text.setText("Tic Tac Toe, O's Turn");

    // Group elements in a panel
    panel = new JPanel();
    panel.setLayout(new java.awt.GridLayout(3, 3)); // 3 x 3 grid
    panel.add(button1);
    panel.add(button2);
    panel.add(button3);
    panel.add(button4);
    panel.add(button5);
    panel.add(button6);
    panel.add(button7);
    panel.add(button8);
    panel.add(button9);


    // ContentPane's default layout manager: BorderLayout
    Container contentPane = getContentPane();
    contentPane.add(panel, "Center");
    contentPane.add(text, "North");
    contentPane.add(reset, "South");
    text.setFont(font1);

    // Adding action listeners
        ButtonObserver observer = new ButtonObserver();
        button1.addActionListener(observer);
        button2.addActionListener(observer);
        button3.addActionListener(observer);
        button4.addActionListener(observer);
        button5.addActionListener(observer);
        button6.addActionListener(observer);
        button7.addActionListener(observer);
        button8.addActionListener(observer);
        button9.addActionListener(observer);
        reset.addActionListener(observer);
    }
    // Adding an event to the button pressed
private class ButtonObserver implements ActionListener {

    /**
     * Button listener
     * 
     * @param e trigger
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();

        if (player == 1) {
            player = 2;

            if (source == button1) {
                button1.setEnabled(false);
                button1.setText("X");
                button1.setFont(font2);
            } else if (source == button2) {
                button2.setEnabled(false);
                button2.setText("X");
                button2.setFont(font2);
            } else if (source == button3) {
                button3.setEnabled(false);
                button3.setText("X");
                button3.setFont(font2);
            } else if (source == button4) {
                button4.setEnabled(false);
                button4.setText("X");
                button4.setFont(font2);
            } else if (source == button5) {
                button5.setEnabled(false);
                button5.setText("X");
                button5.setFont(font2);
            } else if (source == button6) {
                button6.setEnabled(false);
                button6.setText("X");
                button6.setFont(font2);
            } else if (source == button7) {
                button7.setEnabled(false);
                button7.setText("X");
                button7.setFont(font2);
            } else if (source == button8) {
                button8.setEnabled(false);
                button8.setText("X");
                button8.setFont(font2);
            } else if (source == button9) {
                button9.setEnabled(false);
                button9.setText("X");
                button9.setFont(font2);
            }
            text.setText("O's Turn ");

        } else if (player == 2) {

            player = 1;

            if (source == button1) {
                button1.setEnabled(false);
                button1.setText("O");
                button1.setFont(font2);
            } else if (source == button2) {
                button2.setEnabled(false);
                button2.setText("O");
                button2.setFont(font2);
            } else if (source == button3) {
                button3.setEnabled(false);
                button3.setText("O");
                button3.setFont(font2);
            } else if (source == button4) {
                button4.setEnabled(false);
                button4.setText("O");
                button4.setFont(font2);
            } else if (source == button5) {
                button5.setEnabled(false);
                button5.setText("O");
                button5.setFont(font2);
            } else if (source == button6) {
                button6.setEnabled(false);
                button6.setText("O");
                button6.setFont(font2);
            } else if (source == button7) {
                button7.setEnabled(false);
                button7.setText("O");
                button7.setFont(font2);
            } else if (source == button8) {
                button8.setEnabled(false);
                button8.setText("O");
                button8.setFont(font2);
            } else if (source == button9) {
                button9.setEnabled(false);
                button9.setText("O");
                button9.setFont(font2);
            }
            text.setText("X's Turn");
        }

        if (ticTac[0][0] == ticTac[0][1] && ticTac[0][1] == ticTac[0][2] && ticTac[0][0] == ticTac[0][2]) {
            text.setText(button1.getText() + " wins!");
        }
        else if (ticTac[1][0]== ticTac[1][1] && ticTac[1][1] == ticTac[1][2] && ticTac[1][0] == ticTac[1][2] ) {
            text.setText(button4.getText() + " wins!");
        }
        else if (ticTac[2][0] == ticTac[2][1] && ticTac[2][1] == ticTac[2][2] && ticTac[2][0] == ticTac[2][2] ) {
            text.setText(button7.getText() + " wins!");
        }
        else if (ticTac[0][0] == ticTac[1][0] && ticTac[1][0] == ticTac[2][0] && ticTac[0][0] == ticTac[2][0] ) {
            text.setText(button1.getText() + " wins!");
        }
        else if (ticTac[0][1] == ticTac[1][1] && ticTac[1][1] == ticTac[2][1] && ticTac[0][1] == ticTac[2][1]) {
            text.setText(button2.getText() + " wins!");
        }
        else if (ticTac[0][2] == ticTac[1][2] && ticTac[1][2] == ticTac[2][2] && ticTac[0][2] == ticTac[2][2] ) {
            text.setText(button3.getText() + " wins!");
        }
        else if (ticTac[0][0] == ticTac[1][1] && ticTac[1][1] == ticTac[2][2] && ticTac[0][0] == ticTac[2][2] ) {
            text.setText(button1.getText() + " wins!");
        }
        else if (ticTac[0][2] == ticTac[1][1] && ticTac[1][1] == ticTac[2][0] && ticTac[0][2] == ticTac[2][0]) {
            text.setText(button3.getText() + " wins!");
        }
        else {
        text.setText("It's a draw");}

        if (source == reset) {
            text.setText("Tic Tac Toe, O's Turn");
            button1.setEnabled(true);
            button2.setEnabled(true);
            button3.setEnabled(true);
            button4.setEnabled(true);
            button5.setEnabled(true);
            button6.setEnabled(true);
            button7.setEnabled(true);
            button8.setEnabled(true);
            button9.setEnabled(true);
            button1.setText("");
            button2.setText("");
            button3.setText("");
            button4.setText("");
            button5.setText("");
            button6.setText("");
            button7.setText("");
            button8.setText("");
            button9.setText("");
        }

    }
}

}

问题可能出在你的 if 语句上。
因为如果单击按钮,您将更改按钮文本。
尝试

if(button1.getText().equals("X")&&button2getText().equals("X")&&button3.getText().equals("X")){
text.setText(button1.getText() + " wins!");
}