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

发布于 2024-09-29 04:03:15 字数 122 浏览 3 评论 0原文

例如,我有一个带有首选项按钮的窗口。 我想这样做,以便当用户按下首选项按钮并检查他/她的适当选项并按确定时,它会保存首选项,然后当用户在主窗口上按下运行时,它会相应地运行用户在首选项上更改的首选项窗户。

先感谢您。

For example, I have a window with a preference button.
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 window.

Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月隐月明月朦胧 2024-10-06 04:03:15

您可以使用 java.util.prefs< /a> 包。一个简单的例子:

// 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"

java2s.com 还有更多示例。

You can use java.util.prefs package. A simple example:

// 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"

There are many more examples at java2s.com.

寂寞花火° 2024-10-06 04:03:15

有一个专门用于此目的的 Java Preferences API目的。它允许您以简单的跨平台方式存储每个用户的首选项,而 API 本身负责存储数据的位置和方式。

There is a Java Preferences API specifically for this purpose. It lets you store per-user preferences in an easy cross-platform way, while the API itself takes care of where and how to store the data.

淡淡離愁欲言轉身 2024-10-06 04:03:15
public void saveProperties() {
    try {            
        String USER_NAME = "Some name";
        String DP_ADDRESS = "Some url";
        //create a properties file
        Properties props = new Properties();
        props.setProperty("User name", USER_NAME);
        props.setProperty("Display picture address", DP_ADDRESS);
        File f = new File("YOUR_TARGET_FILE_PATH");
        OutputStream out = new FileOutputStream( f );
        //If you wish to make some comments 
        props.store(out, "User properties");
    }
    catch (Exception e ) {
        e.printStackTrace();
    }
}

您可以使用java.util.Properties来保存您的首选项

public void saveProperties() {
    try {            
        String USER_NAME = "Some name";
        String DP_ADDRESS = "Some url";
        //create a properties file
        Properties props = new Properties();
        props.setProperty("User name", USER_NAME);
        props.setProperty("Display picture address", DP_ADDRESS);
        File f = new File("YOUR_TARGET_FILE_PATH");
        OutputStream out = new FileOutputStream( f );
        //If you wish to make some comments 
        props.store(out, "User properties");
    }
    catch (Exception e ) {
        e.printStackTrace();
    }
}

You may use java.util.Properties to save your preferences

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文