我正在尝试创建“井字游戏”。我选择创建JPanel的变体来表示每个正方形。下面的类代表构成我的游戏板的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));
    }
}

最佳答案

在运行时添加/删除组件时,请始终在之后调用revalidate()revalidate()使组件刷新/重新布局。

因此,只需在添加标签后调用revalidate(),它就会起作用。

09-30 13:35