JPanel 对 MouseEvents 没有反应?

JPanel doesn't react to MouseEvents?

我正在尝试创建一个 "Tic Tac Toe" 游戏。我选择创建 JPanel 的变体来表示每个正方形。下面的 class 代表组成我的游戏板的 9 个面板之一。
现在我遇到的问题是,当我单击面板时,'X' 应该显示在面板内部,但没有任何反应。如果有人引导我朝着正确的方向前进,我将不胜感激。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TicTacToePanel extends JPanel implements MouseListener {
    private boolean isPlayer1Turn = true;
    private boolean isUsed = false;
    private JLabel ticTacLbl = new JLabel();

    public TicTacToePanel() {
        setBorder(BorderFactory.createLineBorder(Color.BLACK));
        addMouseListener(this);
    }

    public void mouseClicked(MouseEvent e) {
        if (!isUsed) {
            if (isPlayer1Turn) {
                ticTacLbl.setForeground(Color.red);
                ticTacLbl.setText("X");
                add(ticTacLbl, 0);
                isUsed = true;
            } else {
                ticTacLbl.setForeground(Color.blue);
                ticTacLbl.setText("O");
                add(ticTacLbl, 0);
                isUsed = true;
            }

        } else {

        }
    }

    public void mousePressed(MouseEvent e) {

    }

    public void mouseReleased(MouseEvent e) {

    }

    public void mouseEntered(MouseEvent e) {

    }

    public void mouseExited(MouseEvent e) {

    }

    public static void main(String[] args) {
        JOptionPane.showMessageDialog(null, new TicTacToePanel());
    }

}

编辑:
我只是在我的 TicTacToePanel 的构造函数中添加了我的标签组件,这样我就不再需要调用 revalidate() 并且我不会在运行时添加组件。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TicTacToePanel extends JPanel implements MouseListener{
    private boolean isPlayer1Turn = true;
    private boolean isUsed = false;
    private JLabel ticTacLbl = new JLabel();

    public TicTacToePanel(){
        add(ticTacLbl, 0);
        setBorder(BorderFactory.createLineBorder(Color.BLACK));
        addMouseListener(this);
    }

    public void mouseClicked(MouseEvent e){

    }

    public void mousePressed(MouseEvent e){
        if (!isUsed) {
            if (isPlayer1Turn) {
                ticTacLbl.setForeground(Color.red);
                ticTacLbl.setText("X");
                isUsed = true;
            } else {
                ticTacLbl.setForeground(Color.blue);
                ticTacLbl.setText("O");
                isUsed = true;
            }

        }
        else{

        }

    }

     public void mouseReleased(MouseEvent e){

    }

    public void mouseEntered(MouseEvent e){

    }

    public void mouseExited(MouseEvent e){

    }   

     public static void main(String[] args){
        JOptionPane.showMessageDialog(null, new TicTacToePanel());
    }
}

GUI 构造函数:

     public TicTacToeGUI(int gameMode){
        if(gameMode == 0){
            amountOfPanels = 9;
            TicTacToePanel[] panelArr = new TicTacToePanel[amountOfPanels];
            add(gamePanel, new GridLayout(3, 3));
            setPreferredSize(new Dimension(100, 100));
            for(int i = 0; i < amountOfPanels; i++){
                panelArr[i] = new TicTacToePanel();
                gamePanel.add(panelArr[i]);
            }   
        }
        else if(gameMode == 1){
            amountOfPanels = 225;
            TicTacToePanel[] panelArr = new TicTacToePanel[amountOfPanels];
            add(gamePanel, new GridLayout(15, 15));
            setPreferredSize(new Dimension(500, 500));
            for(int i = 0; i < amountOfPanels; i++){
                panelArr[i] = new TicTacToePanel();
                gamePanel.add(panelArr[i]);
            }
        }

    }

    public static void main(String[] args){
        JOptionPane.showMessageDialog(null, new TicTacToeGUI(0));
    }
}

当您在运行时 add/remove 组件时,总是在之后调用 revalidate()revalidate() 使组件 refresh/relayout.

所以只需在添加标签后调用 revalidate() 即可。

如果您的目标是创建 Tic Tac Toe 游戏,那么您可能希望重新考虑当前将组件动态添加到 GUI 的策略。更好的方法是创建一个组件网格,比如 JLabel,然后在程序启动时将它们放在 JPanel 上。通过这种方式,您可以更改按下的 JLabel 的文本和颜色,如果您想在程序 运行 期间变得花哨,甚至可以更改其图标,而无需添加或删除组件。例如:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

@SuppressWarnings("serial")
public class TicTacToePanel extends JPanel {
    private static final int ROWS = 3;
    private static final int MY_C = 240;
    private static final Color BG = new Color(MY_C, MY_C, MY_C);
    private static final int PTS = 60;
    private static final Font FONT = new Font(Font.SANS_SERIF, Font.BOLD, PTS);
    public static final Color X_COLOR = Color.BLUE;
    public static final Color O_COLOR = Color.RED;
    private JLabel[][] labels = new JLabel[ROWS][ROWS];
    private boolean xTurn = true;

    public TicTacToePanel() {
        setLayout(new GridLayout(ROWS, ROWS, 2, 2));
        setBackground(Color.black);

        MyMouse myMouse = new MyMouse();
        for (int row = 0; row < labels.length; row++) {
            for (int col = 0; col < labels[row].length; col++) {
                JLabel label = new JLabel("     ", SwingConstants.CENTER);
                label.setOpaque(true);
                label.setBackground(BG);
                label.setFont(FONT);
                add(label);
                label.addMouseListener(myMouse);
            }
        }
    }

    private class MyMouse extends MouseAdapter {
        @Override // override mousePressed not mouseClicked
        public void mousePressed(MouseEvent e) {
            JLabel label = (JLabel) e.getSource();
            String text = label.getText().trim();
            if (!text.isEmpty()) {
                return;
            }
            if (xTurn) {
                label.setForeground(X_COLOR);
                label.setText("X");
            } else {
                label.setForeground(O_COLOR);
                label.setText("O");
            }

            // information to help check for win
            int chosenX = -1;
            int chosenY = -1;
            for (int x = 0; x < labels.length; x++) {
                for (int y = 0; y < labels[x].length; y++) {
                    if (labels[x][y] == label) {
                        chosenX = x;
                        chosenY = y;
                    }
                }
            }
            // TODO: check for win here
            xTurn = !xTurn;
        }
    }

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

        JFrame frame = new JFrame("Tic Tac Toe");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}