简体   繁体   English

Java我的应用程序再次启动后如何保存背景色

[英]Java how do I save a background color once my application has been launched again

I'm trying to create a button in game where the background color will go from light_gray to dark_gray. 我正在尝试在游戏中创建一个按钮,其中背景颜色将从light_gray变为dark_gray。 However when the application relaunches I have to re select the button to get the color back to dark_gray. 但是,当应用程序重新启动时,我必须重新选择按钮以使颜色恢复为dark_gray。

How would I have it so that it saves the color when the application is relaunched? 重新启动应用程序时,我将如何保存颜色?

My code is very simple and is just an action listener on the button which then changes the bg color of selected items. 我的代码非常简单,只是按钮上的一个动作侦听器,它随后更改所选项目的bg颜色。

Ok, I have now had the chance to allow it to create the properties file but one doesn't know how one could store the data. 好的,我现在有机会允许它创建属性文件,但是人们不知道如何存储数据。 I've seen people have stuff such as 'properties.setProperty("Favorite sport", "Football");' 我见过人们有一些东西,例如'properties.setProperty(“ Favorite sport”,“ Football”);' But how could one have this so that it stores the bg color? 但是,人们怎么可能拥有它来存储bg颜色呢​​?

windowDark.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae) 
                {
                    try {
                        Properties properties = new Properties();
                        properties.setProperty();

                        File file = new File("DarkTheme.properties");
                        FileOutputStream fileOut = new FileOutputStream(file);
                        properties.store(fileOut, "Dark theme background colour");
                        fileOut.close();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });

The java.util.prefs Preferences API is well suited for storing persistent preference data for user applications running on the desktop. java.util.prefs首选项API非常适合为在桌面上运行的用户应用程序存储持久首选项数据。

Here's an example how you can use it to store and retrieve persistent background color settings: 这是一个示例,您可以使用它来存储和检索持久的背景颜色设置:

import java.awt.Color;
import java.util.prefs.Preferences;

public class MyPrefs {
    private static Preferences preferences = 
                     Preferences.userRoot().node("myappname.ColorPreferences");

    public static void storeBackground(Color background) {
        preferences.putInt("background", background.getRGB());
    }

    public static Color retrieveBackground() {
        // Second argument is the default when the setting has not been stored yet
        int color = preferences.getInt("background", Color.WHITE.getRGB()); 
        return new Color(color);
    }
}

To call it, use something like: 要调用它,请使用类似:

public static void main(String[] args) {
    System.out.println("Background: " + retrieveBackground());
    storeBackground(Color.RED);
}

You can store the color as an int value in the properties file, as follows: 您可以将颜色作为int值存储在属性文件中,如下所示:

windowDark.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
        getProperties().setProperty("color", Integer.toString(getColor().getRGB()));
    }
});

Have the properties as a member of the window this button is in, or even better, in some general location of the application (the class with the main() perhaps ?), and access it with getProperties() . 将属性作为该按钮的窗口成员,放在应用程序的某个常规位置(甚至更好)中(或者更好的是具有main()的类),然后使用getProperties()对其进行访问。

When you need to use the color, parse the string: 当您需要使用颜色时,解析字符串:

Color color = new Color(Integer.parseInt(getProperties().getProperty("color")));

Don't save the properties file on each button click, instead, do so when the application is about to exit: 不要在每次单击按钮时保存属性文件,而是在应用程序即将退出时保存属性文件:

mainWindow.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
mainWindow.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
        try {    
            File file = new File("DarkTheme.properties");
            FileOutputStream fileOut = new FileOutputStream(file);
            getProperties().store(fileOut, "Dark theme background colour");
            fileOut.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            mainWindow.dispose();
        }
    }
});

The change done in memory will be disposed once the application is terminated. 一旦终止应用程序,将完成在内存中所做的更改。 If you want to persist some data (in this case, the background color), then you need to store is somewhere, eg file, database, etc. For a simple application, storing your data in a file will be practical. 如果要保留某些数据(在这种情况下为背景色),则需要将其存储在某个位置,例如文件,数据库等。对于简单的应用程序,将数据存储在文件中将是可行的。 To do this, you will need to: - when application starts, read the file, and apply the color specified in the file - while the application is running and user changes the color, save the color to the same file To deal with file, you will need to use File, FileReader, and FileWriter classes (all are in java.io package). 为此,您需要:-当应用程序启动时,读取文件并应用文件中指定的颜色-在应用程序运行且用户更改颜色时,将颜色保存到同一文件中以处理文件,您将需要使用File,FileReader和FileWriter类(均在java.io包中)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM