gpt4 book ai didi

java - JMenuItems 没有响应

转载 作者:行者123 更新时间:2023-12-01 18:00:46 26 4
gpt4 key购买 nike

我创建了一个带有颜色的 3x3 内存游戏。程序运行并显示颜色,但 JMenuBar 无法正常工作。新棋盘、游戏和结束游戏按钮不起作用。我的 Actionlistener 有问题吗?

这就是代码应该如何工作:https://gyazo.com/9bf3e073a9c455e56d9b4403586bfaf5?fbclid=IwAR3Zk1BZvRj49CtAz01h95zic8tk74UmyUcU2HrY3VY8XcARoD14Ke6tcoQ

这是StartPlayer

import gui.MemoryGameWindow;
import gui.ColoredPanel;
import gui.GamePanel;

public class StartPlayer {
public static void main(String[] args) {
new MemoryGameWindow();

}
}

这是ColoredPanel

package gui;

import java.awt.Color;
import java.awt.event.MouseListener;
import javax.swing.JPanel;

public class ColoredPanel extends JPanel {
private Color thisColor = null;

private Color challenge = null;

public ColoredPanel(Color color) {
this.thisColor = color;
setBackground(color);
}

public void setGameMode(Color c, MouseListener ml) {
setBackground(Color.LIGHT_GRAY);
this.challenge = c;
System.out.println("Coloredpanel says challenge is " + this.challenge);
addMouseListener(ml);
}

public void restoreBackground() {
setBackground(this.thisColor);
}
}

这是GamePanel

package gui;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class GamePanel extends JPanel {
private Color[] colors = new Color[] { Color.GREEN, Color.YELLOW, Color.MAGENTA };

private Random r = new Random();

private int square = 9;

private Color challenge;

private int countOfPossibleHits = 0;

private int countOfWinnerHits = 0;

private JPanel challengeDisplay;

public GamePanel(JPanel cd) {
this.challengeDisplay = cd;
setLayout(new GridLayout(3, 0, 2, 2));
for (int i = 0; i < this.square; i++)
add(new ColoredPanel(this.colors[this.r.nextInt(this.colors.length)]));
}

public Color startGame() {
Color[] existingColors = new Color[getComponentCount()];
int i;
for (i = 0; i < getComponentCount(); i++)
existingColors[i] = ((ColoredPanel)getComponent(i)).getBackground();
this.challenge = existingColors[this.r.nextInt(existingColors.length)];
this.countOfPossibleHits = 0;
for (i = 0; i < getComponentCount(); i++) {
if (((ColoredPanel)getComponent(i)).getBackground() == this.challenge)
this.countOfPossibleHits++;
}
for (i = 0; i < getComponentCount(); i++)
((ColoredPanel)getComponent(i)).setGameMode(this.challenge, new TheMouselistener());
return this.challenge;
}

class TheMouselistener extends MouseAdapter {
public void mousePressed(MouseEvent e) {
ColoredPanel clickedObject = (ColoredPanel)e.getSource();
clickedObject.restoreBackground();
if (clickedObject.getBackground() != GamePanel.this.challenge) {
clickedObject.add(new JLabel("Game Over!"));
GamePanel.this.challengeDisplay.add(new JLabel("Game Over!"));
System.out.println("Game Over");
GamePanel.this.updateUI();
} else {
GamePanel.this.countOfWinnerHits = GamePanel.this.countOfWinnerHits + 1;
if (GamePanel.this.countOfWinnerHits == GamePanel.this.countOfPossibleHits) {
clickedObject.add(new JLabel("You won!"));
GamePanel.this.challengeDisplay.add(new JLabel("You Won!"));
GamePanel.this.updateUI();
}
}
}
}
}

这是MemoryGameWindow

package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

public class MemoryGameWindow extends JFrame implements ActionListener {
private JMenuItem newgame = null;

private JMenuItem playgame = null;

private JMenuItem exit = null;

private GamePanel gamepanel = null;

private JPanel challengeDisplay;

public MemoryGameWindow() {
setTitle("memory game");
setLayout(new BorderLayout(5, 5));
add(this.challengeDisplay = new JPanel() {

}, "North");
add(this.gamepanel = new GamePanel(this.challengeDisplay));
setJMenuBar(new GameMenubar(this));
setSize(600, 600);
setLocationRelativeTo((Component)null);
setVisible(true);
}

public void actionPerformed(ActionEvent e) {
Object s = e.getSource();
if (s == this.newgame) {
remove(this.gamepanel);
remove(this.challengeDisplay);
add(this.challengeDisplay = new JPanel() {

}, "North");
add(this.gamepanel = new GamePanel(this.challengeDisplay));
this.playgame.setEnabled(true);
this.gamepanel.updateUI();
}
if (s == this.playgame) {
Color challenge = this.gamepanel.startGame();
this.challengeDisplay.setBackground(challenge);
this.playgame.setEnabled(false);
this.gamepanel.updateUI();
}
if (s == this.exit)
System.exit(0);
}

class GameMenubar extends JMenuBar {
public GameMenubar(MemoryGameWindow memoryGameWindow) {
JMenu file;
add(file = new JMenu("File"));
MemoryGameWindow.this.newgame = new JMenuItem("new board");
file.add(new JMenuItem("new board"));
MemoryGameWindow.this.playgame = new JMenuItem("play");
file.add(new JMenuItem("play"));
MemoryGameWindow.this.exit = new JMenuItem("end program");
file.add(new JMenuItem("end program"));
MemoryGameWindow.this.newgame.addActionListener(memoryGameWindow);
MemoryGameWindow.this.playgame.addActionListener(memoryGameWindow);
MemoryGameWindow.this.exit.addActionListener(memoryGameWindow);


}
}
}

最佳答案

看一下这两行:

MemoryGameWindow.this.playgame = new JMenuItem("play");
file.add(new JMenuItem("play"));

首先,将 playgame 的值设置为新的 JMenuItem。稍后在代码中,您将向其中添加一个 ActionListener。这都是正确的。

问题是,添加了 ActionListener 的 JMenuItem 并不是您添加到菜单栏的内容。相反,您将添加一个全新 JMenuItem,其中没有添加任何 ActionListener。

修复方法非常简单:

file.add(MemoryGameWindow.this.playgame);

显然,您也需要对其他菜单项执行此操作。

关于java - JMenuItems 没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60635365/

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