我有一个显示在程序开头的帮助窗格,但可以关闭。如果用户希望它返回,则菜单栏中有一个选项可以重新激活它。但是,当他们选择从帮助菜单中显示它时,它会自动重新检查“不再显示”框。如何将盒子保持在用户最初使用的状态,但仍然打开帮助窗格?
桂:
public class Gui {
private Game game;
private JFrame frame;
private MenuBar menuBar;
private HelpDialog helpMenu;
private boolean showHelp;
public Gui(Game game) {
this.game = game;
this.showHelp = true;
this.createAndShowGUI();
}
public boolean shouldShowHelpDialog() {
return this.showHelp;
}
public void displayHelp() {
this.helpMenu.showHelpDialog();
}
的MenuBar:
public class MenuBar {
private JMenuBar menuBar;
private JMenu menu;
private JMenuItem menuItem;
private JFrame frame;
private Gui gui;
private Game game;
public MenuBar(JFrame frame, Gui gui, Game game) {
this.menuBar = new JMenuBar();
this.frame = frame;
this.gui = gui;
this.game = game;
}
public void buildMenuBar() {
this.buildFileMenu();
this.buildSettingsMenu();
this.buildHelpMenu();
this.frame.setJMenuBar(this.menuBar);
}
private void buildHelpMenu() {
this.menu = new JMenu("Information");
this.menu.setMnemonic(KeyEvent.VK_I);
this.menu.getAccessibleContext().setAccessibleDescription("Help menu");
JMenuItem menuHelp = new JMenuItem("Help", KeyEvent.VK_H);
menuHelp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
MenuBar.this.gui.displayHelp();
}
});
this.menu.add(menuHelp);
this.menuBar.add(this.menu);
}
HelpDialog:
public class HelpDialog {
private boolean shouldShowHelpDialog;
private JFrame theFrame;
public HelpDialog(boolean helpDialog, JFrame frame) {
this.shouldShowHelpDialog = helpDialog;
this.theFrame = frame;
}
public boolean showHelpDialog() {
if (!this.shouldShowHelpDialog) {
return false;
}
JCheckBox shouldShowCheckBox = new JCheckBox("Do not show this message again", this.shouldShowHelpDialog);
Object[] msgContent = { this.buildHelpPane(), shouldShowCheckBox };
JOptionPane.showMessageDialog(this.theFrame, msgContent, "Help", JOptionPane.INFORMATION_MESSAGE);
return shouldShowCheckBox.isSelected();
}
private Object buildHelpPane() {
String helpMessage = "Game rules: This is how you play.";
JTextArea helpTextArea = new JTextArea(helpMessage);
helpTextArea.setRows(6);
helpTextArea.setColumns(40);
helpTextArea.setLineWrap(true);
helpTextArea.setWrapStyleWord(true);
helpTextArea.setEditable(false);
helpTextArea.setOpaque(false);
JScrollPane helpPane = new JScrollPane(helpTextArea);
return helpPane;
}
}
编辑:
更新了HelpDialog类:
public class HelpDialog {
private boolean shouldShowHelpDialog;
private JFrame theFrame;
private JCheckBox shouldShowCheckBox;
public HelpDialog(boolean helpDialog, JFrame frame) {
this.shouldShowHelpDialog = helpDialog;
this.theFrame = frame;
this.shouldShowCheckBox = new JCheckBox("Do not show this message again", this.shouldShowHelpDialog);
}
public boolean showHelpDialog() {
if (!this.shouldShowHelpDialog) {
return false;
}
Object[] msgContent = { this.buildHelpPane(), shouldShowCheckBox };
JOptionPane.showMessageDialog(this.theFrame, msgContent, "Help", JOptionPane.INFORMATION_MESSAGE);
return shouldShowCheckBox.isSelected();
}
当通过菜单栏显示帮助菜单时,此复选框仍然未标记。但是,现在创建新游戏时,即使取消选中此框,它也会显示帮助对话框。
完整答案包括对GUI中方法的更改:
public void displayHelp() {
this.showHelp = this.helpMenu.showHelpDialog();
}
答案 0 :(得分:4)
每次调用showHelpDialog()方法时都会创建一个新的复选框。您应该在构造函数中创建一次对话框,showHelpDialog()应该只显示它。
答案 1 :(得分:0)
您可以向showHelpDialog添加一个覆盖您的请求的参数
public boolean showHelpDialog(boolean override) {
if(!override){
if (!this.shouldShowHelpDialog) {
return false;
}
}
JCheckBox shouldShowCheckBox = new JCheckBox("Do not show this message again", this.shouldShowHelpDialog);
Object[] msgContent = { this.buildHelpPane(), shouldShowCheckBox };
JOptionPane.showMessageDialog(this.theFrame, msgContent, "Help", JOptionPane.INFORMATION_MESSAGE);
return shouldShowCheckBox.isSelected();
}
并致电
showHelpDialog(true);
从菜单中点击。