Java 无法保存静态复选框的状态

Java 无法保存静态复选框的状态,java,swing,static,preferences,Java,Swing,Static,Preferences,我创建了一个小的设置菜单,它通过单击保存和退出来保存复选框的状态,当然,通过单击取消来忘记状态 我需要对复选框的引用,因此我必须使用static。但是当我使用它时,save state函数将不再正常工作…为什么( public void load();应该包装在invokeLater中,有关在Swing-Initial Thread中阅读Oracle trailConcurrency的更多信息,请参见@mKorbel“我需要对复选框的引用,所以我必须使用static”。此语句是错误的。我们可以为

我创建了一个小的设置菜单,它通过单击保存和退出来保存复选框的状态,当然,通过单击取消来忘记状态

我需要对复选框的引用,因此我必须使用static。但是当我使用它时,save state函数将不再正常工作…为什么(


public void load();
应该包装在
invokeLater
中,有关在Swing-Initial Thread中阅读Oracle trail
Concurrency的更多信息,请参见@mKorbel“我需要对复选框的引用,所以我必须使用static”。此语句是错误的。我们可以为复选框(或其状态)提供getter方法.首先修复
静态
。。
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.util.prefs.Preferences;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

public class SettingsGui extends JPanel {

    static Preferences PREFS = Preferences.userNodeForPackage(SettingsGui.class);
    JCheckBox testAllPagesHaveSameRotation;
    JCheckBox testAllPagesHaveSameSize;
    JCheckBox testContent;
    JTabbedPane tabbedPane;
    ButtonGroup buttonGroup;

    public SettingsGui() {
        setPreferredSize(new Dimension(500, 500));
        setLayout(new BorderLayout());
        tabbedPane = new JTabbedPane(SwingConstants.LEFT, JTabbedPane.WRAP_TAB_LAYOUT);
        tabbedPane.setFont(new Font("Arial", Font.BOLD, 13));
        JPanel pdfCheckOptions = new JPanel(new GridLayout(3, 1));
        testAllPagesHaveSameRotation = new JCheckBox("testAllPagesHaveSameRotation");
        testAllPagesHaveSameRotation.setFont(new Font("Arial", Font.PLAIN, 15));
        testAllPagesHaveSameSize = new JCheckBox("All pages have same size");
        testAllPagesHaveSameSize.setFont(new Font("Arial", Font.PLAIN, 15));
        testContent = new JCheckBox("Content");
        testContent.setFont(new Font("Arial", Font.PLAIN, 15));
        tabbedPane.addTab("Check Settings", pdfCheckOptions);
        pdfCheckOptions.add(testAllPagesHaveSameRotation);
        pdfCheckOptions.add(testAllPagesHaveSameSize);
        pdfCheckOptions.add(testContent);
        add(tabbedPane);
        load();
    }

    public void store() {
        PREFS.putBoolean("test1", testAllPagesHaveSameRotation.isSelected());
        PREFS.putBoolean("test2", testAllPagesHaveSameSize.isSelected());
        PREFS.putBoolean("test3", testContent.isSelected());


    public void load() {
        testAllPagesHaveSameRotation.setSelected(PREFS.getBoolean("test1", false));
        testAllPagesHaveSameSize.setSelected(PREFS.getBoolean("test2", false));
        testContent.setSelected(PREFS.getBoolean("test3", false));
    }

    class Gui {

        JDialog settingsDialog;
        SettingsGui content;
        JButton saveButton;
        JButton cancelButton;
        JPanel buttonPanel;

        public Gui() {
            content = new SettingsGui();
            settingsDialog = new JDialog(settingsDialog, "Settings");
            settingsDialog.setContentPane(content);
            settingsDialog.setLocationRelativeTo(null);
            settingsDialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
            settingsDialog.pack();
            settingsDialog.setVisible(true);
            buttonPanel = new JPanel();
            buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
            saveButton = new JButton("Save and Quit");
            cancelButton = new JButton("Cancel");
            buttonPanel.add(saveButton);
            buttonPanel.add(cancelButton);
            settingsDialog.add(buttonPanel, BorderLayout.SOUTH);
            saveButton.addActionListener(new java.awt.event.ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    content.store();
                    settingsDialog.dispose();

                }
            });
            cancelButton.addActionListener(new java.awt.event.ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    PREFS.putBoolean("test1", testAllPagesHaveSameRotation.isSelected());
                    PREFS.putBoolean("test2", testAllPagesHaveSameSize.isSelected());
                    PREFS.putBoolean("test3", testContent.isSelected());
                    settingsDialog.dispose();
                }
            });
        }
    }

    public static void main(String[] args) {
        new SettingsGui().new Gui();
    }
}