我设置了两个J菜单项,一个是“新游戏”,另一个是“关于游戏”。但是,当我运行程序并按下“新游戏”时,会显示“关于游戏”的对话框,那么如何解决这个问题呢?
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();
}发布于 2018-05-02 17:43:37
排在队伍里
if (arg0.getActionCommand().equals("About the game")); aboutGame();在if语句之后有分号。从本质上说,这将if语句简化为不带主体的if语句。因此,jvm将处理它是真还是假,丢弃结果,并移到下一行,即aboutGame()行。如果你把它去掉,问题就应该解决了。顺便说一句,省略大括号从来都不是个好主意,即使在一行if语句上也是如此。
if (arg0.getActionCommand().equals("About the game")) {
aboutGame();
}https://stackoverflow.com/questions/50140570
复制相似问题