为什么每个按钮的全局变量重置或不同?

Why is the global variable resetting or different for each button?

我正在制作一个井字游戏,我希望能够在单击时让按钮交替显示 x 和 o。现在它们在第一次点击时都是 x,在第二次点击时都是 o。我也尝试过使用和不使用关键字 this。

这是按钮class

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;
   }
  }
}

这是棋盘class

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);
 }
}

您的问题是无效的对象设计之一:每个按钮都有自己的布尔 x 状态,它独立于所有其他状态,您不应该这样做。事实上,您应该有一个 Game 对象,一个保存按钮并且与按钮分开的对象,它保存此状态并根据此状态控制按钮对按下的响应。

旁注:我不会扩展 JButton,而是使用 JButton。

例如,您可以为所有 JButton 提供 相同的 ActionListener:

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;    
import javax.swing.*;

public class TicTacFoo extends JPanel {
    private static final long serialVersionUID = 1L;
    private static final int ROWS = 3;
    private static final Font BTN_FONT = new Font(Font.SANS_SERIF, Font.BOLD, 80);
    private JButton[][] buttons = new JButton[ROWS][ROWS];
    private boolean xTurn = true;
    private int count = 0;

    public TicTacFoo() {

        JPanel buttonPanel = new JPanel(new GridLayout(ROWS, ROWS));
        setLayout(new GridLayout(ROWS, ROWS));

        // single ActionListener for all buttons
        ButtonListener buttonListener = new ButtonListener();

        // create buttons in nested for loop
        for (int row = 0; row < buttons.length; row++) {
            for (int col = 0; col < buttons[row].length; col++) {
                buttons[row][col] = new JButton("   ");
                buttons[row][col].setFont(BTN_FONT);
                buttons[row][col].addActionListener(buttonListener);
                buttonPanel.add(buttons[row][col]);
            }
        }

        setLayout(new BorderLayout());
        add(buttonPanel);
        add(new JButton(new ResetAction()), BorderLayout.PAGE_END);
    }

    private class ButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {

            // get the button that was pressed
            JButton buttonPressed = (JButton) e.getSource();
            String text = buttonPressed.getText().trim();
            if (!text.isEmpty()) {
                // button already has been pressed
                // so exit from the listener
                return;
            }

            if (xTurn) {
                buttonPressed.setText("X");
            } else {
                buttonPressed.setText("O");
            }

            int rowPressed = -1;
            int colPressed = -1;
            // which button pressed?
            for (int row = 0; row < buttons.length; row++) {
                for (int col = 0; col < buttons[row].length; col++) {
                    if (buttons[row][col] == buttonPressed) {
                        rowPressed = row;
                        colPressed = col;
                        break;
                    }
                }
            }

            // TODO: here code where you would test for win
            // ......

            // swap turn
            xTurn = !xTurn;
            // increment count:
            count++;
            System.out.printf("count: %d, [row, col]: [%d, %d]%n", count, rowPressed, colPressed);
        }
    }

    // resets program back to initial state
    @SuppressWarnings("serial")
    class ResetAction extends AbstractAction {
        public ResetAction() {
            super("Reset");
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            // loop through all buttons, resetting state back to initial
            for (JButton[] buttonRow : buttons) {
                for (JButton button : buttonRow) {
                    button.setText("   ");
                }
            }
            xTurn = true;
            count = 0;
        }
    }

    private static void createAndShowGui() {
        TicTacFoo mainPanel = new TicTacFoo();

        JFrame frame = new JFrame("TicTacFoo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

这不是 C++ 而这一个

boolean x = true;

不是 Java 中的 global。要在 Java 中模仿可以理解为 "global"(所有 class 实例通用)的变量,您需要将其声明为 static like

static boolean x = true;