JCheckBox 状态在 类 之间保持一致

JCheckBox State Remain Consistent Among Classes

我有一个帮助窗格,它出现在程序开始时,但可以关闭。如果用户希望它 return,菜单栏中有一个选项可以重新激活它。但是,当他们选择从帮助菜单中显示它时,它会自动重新选中 "do not show again" 框。如何使框保持用户最初的状态,但仍打开帮助窗格?

贵:

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();
    }

菜单栏:

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);
    }

帮助对话框:

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;
    }
}

编辑:

更新了帮助对话框class:

    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();
}

您的 showHelpDialog() 方法在每次调用时都会创建一个新的复选框。您应该在构造函数中创建一次对话框,showHelpDialog() 应该只显示它​​。

您可以向 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);

从菜单中单击时。