首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >JMenu重复显示showMessageDialog

JMenu重复显示showMessageDialog
EN

Stack Overflow用户
提问于 2018-05-02 17:40:24
回答 1查看 30关注 0票数 1

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

代码语言:javascript
运行
复制
 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();


}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-02 17:43:37

排在队伍里

代码语言:javascript
运行
复制
if (arg0.getActionCommand().equals("About the game")); aboutGame();

在if语句之后有分号。从本质上说,这将if语句简化为不带主体的if语句。因此,jvm将处理它是真还是假,丢弃结果,并移到下一行,即aboutGame()行。如果你把它去掉,问题就应该解决了。顺便说一句,省略大括号从来都不是个好主意,即使在一行if语句上也是如此。

代码语言:javascript
运行
复制
if (arg0.getActionCommand().equals("About the game")) {
    aboutGame();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50140570

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档