gpt4 book ai didi

java - JMenu反复显示showMessageDialog

转载 作者:太空宇宙 更新时间:2023-11-04 10:29:50 25 4
gpt4 key购买 nike

我设置了2个Jmenu项,一个是“新游戏”,另一个是“关于游戏”。但是,当我运行程序并按“新游戏”时,会显示“关于游戏”对话框,那么我该如何解决这个问题呢?

 public Game() {
JMenuBar menuBar = new JMenuBar();
this.mainFrame.setJMenuBar(menuBar);
JMenu aMenu = new JMenu ("New Game");
menuBar.add(aMenu);
newMenuItem("New Game", aMenu, this);
JMenu bMenu = new JMenu("About");
menuBar.add(bMenu);
newMenuItem("About the game",bMenu,this);

}


public void aboutGame () {
final String AboutGameText =
" The game is about...";
JOptionPane.showMessageDialog(this.mainFrame, AboutGameText, "About the game", JOptionPane.PLAIN_MESSAGE);

}



public void actionPerformed(ActionEvent arg0) {
if (arg0.getActionCommand().equals("New Game")) Game();
if (arg0.getActionCommand().equals("About the game")); aboutGame();


}

最佳答案

在行

if (arg0.getActionCommand().equals("About the game")); aboutGame();

if 语句后有一个分号。本质上,这将 if 语句简化为没有主体的 if 语句。因此,jvm 将处理 true 或 false,丢弃结果,然后移至下一行,即 aboutGame() 行。如果删除它,问题就会消失。顺便说一句,省略大括号从来都不是一个好主意,即使在一行 if 语句中也是如此

if (arg0.getActionCommand().equals("About the game")) {
aboutGame();
}

关于java - JMenu反复显示showMessageDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50140570/

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