gpt4 book ai didi

java - JCheckBox 状态在类之间保持一致

转载 作者:行者123 更新时间:2023-12-01 18:03:55 25 4
gpt4 key购买 nike

我有一个帮助 Pane ,它出现在程序开始时,但可以关闭。如果用户希望它返回,菜单栏中有一个选项可以重新激活它。但是,当他们选择从帮助菜单中显示它时,它会自动重新选中“不再显示”框。如何使该框保持与用户最初的状态相同,但仍打开帮助 Pane ?

图形用户界面:

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

编辑:

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

最佳答案

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

关于java - JCheckBox 状态在类之间保持一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38378443/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com