我正在构建一个小程序,它有一个设置屏幕,用户可以在其中选择颜色主题。我为每种颜色使用了一个JRadioButton,但是在下次运行程序时我无法让程序保存它。这是我的代码:
private Preferences userPreferences = Preferences.userRoot();
MainWindow() {
super("Timer");
setLayout(new GridLayout(4,3,5,5));
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(650,290);
setResizable(false);
userPreferences.get("COLOR_CODE", "#ededed");
//MORE CODE HERE
@Override
public void menuSelected(MenuEvent e) {
//Open settings window
if(e.getSource().equals(settings)) {
about.setEnabled(false);
exit.setEnabled(false);
settingsFrame = new SettingsWindow();
settingsFrame.setAlwaysOnTop(true);
settingsFrame.setLocationRelativeTo(null);
settingsFrame.setVisible(true);
//WindowListener for closing the settings window
settingsFrame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent we) {
settings.setEnabled(true);
about.setEnabled(true);
exit.setEnabled(true);
}
});
//Adding action listeners to the radio buttons
settingsFrame.defaultColorRB.addActionListener(ee -> {
settingsFrame.setColor("#ededed");
getContentPane().setBackground(Color.decode(settingsFrame.getColorCode()));
userPreferences.put("COLOR_CODE", settingsFrame.getColorCode());
});
settingsFrame.whiteColorRB.addActionListener(ee -> {
settingsFrame.setColor("#FFFFFF");
getContentPane().setBackground(Color.decode(settingsFrame.getColorCode()));
userPreferences.put("COLOR_CODE", settingsFrame.getColorCode());
});
settingsFrame.lightGrayColorRB.addActionListener(ee -> {
settingsFrame.setColor("#D2D8DF");
getContentPane().setBackground(Color.decode(settingsFrame.getColorCode()));
userPreferences.put("COLOR_CODE", settingsFrame.getColorCode());
});
settingsFrame.darkGrayColorRB.addActionListener(ee -> {
settingsFrame.setColor("#A2A4A6");
getContentPane().setBackground(Color.decode(settingsFrame.getColorCode()));
userPreferences.put("COLOR_CODE", settingsFrame.getColorCode());
});
settingsFrame.yellowColorRB.addActionListener(ee -> {
settingsFrame.setColor("#FBFF00");
getContentPane().setBackground(Color.decode(settingsFrame.getColorCode()));
userPreferences.put("COLOR_CODE", settingsFrame.getColorCode());
});
settingsFrame.pinkColorRB.addActionListener(ee -> {
settingsFrame.setColor("#F58EB3");
getContentPane().setBackground(Color.decode(settingsFrame.getColorCode()));
userPreferences.put("COLOR_CODE", settingsFrame.getColorCode());
});
settingsFrame.cyanColorRB.addActionListener(ee -> {
settingsFrame.setColor("#32D0F7");
getContentPane().setBackground(Color.decode(settingsFrame.getColorCode()));
userPreferences.put("COLOR_CODE", settingsFrame.getColorCode());
});
}
}
请任何人都可以告诉我为什么上面的代码不能保存用户的颜色选择?
答案 0 :(得分:1)
如果保存颜色代码(如问题中所示),则需要测试保存的颜色代码并选择相应的按钮。示例,非常基础,仅用于演示:
// assuming userPreferences is java.util.prefs.Preferences
String colorCode = userPreferences.get("COLOR_CODE", "#ededed");
switch (colorCode.toLowerCase()) {
case "#ededed": defaultColorRB.setSelected(true); break;
case "#ffffff": whiteColorRB.setSelected(true); break;
...
default: /* none selected - not sure what you want to do in that case */ break;
}
提示1:为首选项键“COLOR_CODE”使用常量和颜色(例如“ededed”)
提示2:由于您使用的是单选按钮,即一些不同的颜色,因此保存选定的单选按钮索引(或某些常量)而不是颜色代码本身(例如#ededed)会更容易。如果允许用户选择任何颜色,而不仅仅是单选按钮,你必须保存颜色代码。
这是第二个提示的示例:
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.prefs.Preferences;
import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;
import javax.swing.border.TitledBorder;
public class Radio {
private static final String PREF_BUTTON_INDEX = "color button";
private final Preferences prefs = Preferences.userNodeForPackage(getClass());
private static final String[] COLORS = { "#ededed", "#ffffff", "#a00000" };
private List<JRadioButton> buttons;
private JLabel output; // so we can see something, simulating usage of color
private Radio() {
buttons = new ArrayList<>();
SwingUtilities.invokeLater(this::initGUI);
}
private void initGUI() {
JPanel panel = new JPanel();
ButtonGroup group = new ButtonGroup();
for (int i = 0; i < COLORS.length; i++) {
JRadioButton button = new JRadioButton(COLORS[i]);
button.addActionListener(this::buttonSelected);
group.add(button);
panel.add(button);
buttons.add(button);
}
output = new JLabel("undefined");
output.setBorder(new TitledBorder("Color:"));
panel.add(output);
int colorIndex = prefs.getInt(PREF_BUTTON_INDEX, -1);
if (colorIndex != -1) {
buttons.get(colorIndex).setSelected(true);
output.setText(COLORS[colorIndex]);
}
JOptionPane.showMessageDialog(null, panel);
}
private void buttonSelected(ActionEvent ev) {
int index = buttons.indexOf(ev.getSource());
if (index != -1) {
output.setText(COLORS[index]);
prefs.putInt(PREF_BUTTON_INDEX, index);
}
}
public static void main(String[] args) {
new Radio();
}
}
答案 1 :(得分:0)
首先,一个很好的用法是创建节点而不是直接在用户根节点中保存首选项。 试试这个:
// This will define a node in which the preferences can be stored
prefs = Preferences.userRoot().node(this.getClass().getName());
看看这是否有所改变。