我创建了一个小设置菜单,通过单击保存并退出来保存checbox的状态,当然通过单击取消忘记状态。
我需要对复选框的引用,所以我必须使用 static 。但是当我使用它时,保存状态功能将无法正常工作......为什么? :(
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();
}
}