gpt4 book ai didi

java - JPanel 对 MouseEvents 没有反应?

转载 作者:行者123 更新时间:2023-12-01 18:09:51 25 4
gpt4 key购买 nike

我正在尝试创建一个“Tic Tac Toe”游戏。我选择创建 JPanel 的变体来代表每个方 block 。下面的类代表了我的游戏板的 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() 即可。

关于java - JPanel 对 MouseEvents 没有反应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33856588/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com