只有一个应该打开多个messageDialog

时间:2013-06-29 19:35:39

标签: java swing dialog

所以我有一个系统托盘,我有这个代码。

MenuItem help = new MenuItem ("Help");
help.addActionListener(selectedListener);

然后selectedListener的代码是

} else if (pressed.equals("Help")) {
    JOptionPane.showMessageDialog(null, "MESSAGE HERE", "TITLE", 1);
}

问题在于,当我在系统托盘中选择“帮助”项时,messageDialog会出现,但是当我点击“OK”时会出现另一个,我怎么能阻止这种情况发生,只有一个出现甚至在“OK”被击中?

MenuItem help = new MenuItem("Help");
help.addActionListener(selectedListener);//HERE
//
MenuItem exit = new MenuItem("Exit");
//
takeSS.addActionListener(selectedListener);
hotKey.addActionListener(selectedListener);
version.addActionListener(selectedListener);
changeLog.addActionListener(selectedListener);
help.addActionListener(selectedListener);
exit.addActionListener(selectedListener);

1 个答案:

答案 0 :(得分:2)

您将ActionListener - selectedListener 两次添加到帮助 MenuItem

MenuItem help = new MenuItem("Help");
help.addActionListener(selectedListener);//HERE
//
MenuItem exit = new MenuItem("Exit");
//
takeSS.addActionListener(selectedListener);
hotKey.addActionListener(selectedListener);
version.addActionListener(selectedListener);
changeLog.addActionListener(selectedListener);
help.addActionListener(selectedListener);// AND HERE
exit.addActionListener(selectedListener);

删除一个,它将按预期工作。