java - 如何在 Java 中保存首选项用户设置?

标签 java swing jframe jbutton

例如,我有一个带有首选项按钮的窗口。 I want to make it so that when user press the preference button and checks his/her appropriate options and press ok, it saves the preference, then when user presses run on the main window, it runs accordingly to preference the user changed on the preference窗口。

提前谢谢你。

最佳答案

您可以使用 java.util.prefs包裹。一个简单的例子:

// Retrieve the user preference node for the package com.mycompany
Preferences prefs = Preferences.userNodeForPackage(com.mycompany.MyClass.class);

// Preference key name
final String PREF_NAME = "name_of_preference";

// Set the value of the preference
String newValue = "a string";
prefs.put(PREF_NAME, newValue);

// Get the value of the preference;
// default value is returned if the preference does not exist
String defaultValue = "default string";
String propertyValue = prefs.get(PREF_NAME, defaultValue); // "a string"

还有更多 examples at java2s.com .

关于java - 如何在 Java 中保存首选项用户设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4017137/

相关文章:

java - 我可以在 Spring 解析器中返回 RuntimeBeanReference 吗?

java - 将 Swing GUI 添加到现有的工作 java 程序

java - 更改 JSlider 的单位移动

java - 是否可以将 JFrame 放在前面而不是焦点?

Java:如何在显示对话框时禁止单击面板?

java - 设置 JFrame 而不与任务栏重叠

java - Fragment 和 BroadcastReceiver 在几秒钟后卡住应用程序

java - Threadpoolexecutor 不更新并发 HashMap

java - Wicket 附加到现有的 Spring 上下文

java - 从 JTable 动态添加/删除行