从JAR中保存首选项 [英] Saving Preferences from within a JAR
问题描述
我目前正在编写游戏,并且已经到了我需要用户能够保存诸如JFrame大小,键绑定等内容的首选项的位置。用户将通过下载的jar运行游戏来自我的网站。
I am currently writing a game, and have come to the point where I need the user to be able to save preferences for things such as the JFrame size, key bindings etc. The users will be running the game through a jar downloaded from my website.
我决定使用java.util.prefs.Preferences下的Preferences API。在阅读文档等内容时,我仍然很新,并且希望有人帮我解释一下。
I've decided to go with the Preferences API under "java.util.prefs.Preferences". I am still quite new when it comes to reading things like documentation, and would like someone to help explain a bit to me.
到目前为止,我有这样的事情:
So far, I have something like this:
Preferences prefs = Preferences.userNodeForPackage(com.custardgames.horde2d.Game.class);
prefs.put(name, value);
现在,我如何实际保存用户计算机上的首选项文件?现在,如果我再次运行该程序并尝试:
Now then, how do I actually get to saving the preference file on the users computer? Right now, if I run the program again and try just:
Preferences prefs = Preferences.userNodeForPackage(com.custardgames.horde2d.Game.class);
System.out.println(prefs.get(name, null));
它只返回null,如果没有保存的话,这是默认值。我知道我错过了实际保存文件的重要部分,然后以正确的方式打开它,但谷歌搜索没有得到太多。
It just returns null which is the default value if there is nothing saved. I know I am missing the vital part of actually saving the file, and then opening it the correct way, but haven't gotten much out of googling it.
非常感谢提前!
编辑:
仅供参考,为了阅读图像,我是使用类似这样的东西:
FYI, for reading images, I am using something like this:
BufferedImage currentImage=ImageIO.read(this.getClass().getClassLoader().getResource("res/images/image.jpg"));
我希望能够做一些与偏好类似的事情。
I would like to be able to do something similar with preferences.
编辑:
name
和 value
都是事先声明的String变量。
name
and value
are both String variables declared beforehand.
推荐答案
您确定 name
变量具有相同的值吗?因为它应该返回正确的存储值 - 也许值为空?
Are you sure that name
variable has the same value? Because it should return correct stored value - maybe value is null?
尝试在存储之前将它们打印出来&获得。
Try printing them out just before storing & getting.
您还可以尝试在存储后刷新首选项:
You can also try to flush preferences after storing:
prefs.flush();
如果你在Windows上,你可以检查首选项是否存储在 regedit
If you're on Windows you can check if the preferences are stored in regedit
under
\HKEY_CURRENT_USER\Software\JavaSoft\Prefs\<your package path>
我不确定但是你的主目录中应该有一个隐藏文件夹。类似
in Linux I'm not sure but there should be a hidden folder in your home directory. Something like
~/.java/.prefs/<package path>
我开发了一个JFrame,使用首选项存储最后一个位置&框架的大小。您可以在这里查看:
I've developed a JFrame using preferences for storing last position & size of a frame. You can look at it here:
图书馆位于:
java-utils
您可能感兴趣的代码:
public void saveSize() {
Preferences preferences = Preferences.userNodeForPackage(this.getClass());
preferences.put(getId() + X_KEY, String.valueOf(getLocation().x));
preferences.put(getId() + Y_KEY, String.valueOf(getLocation().y));
preferences.put(getId() + W_KEY, String.valueOf(getSize().width));
preferences.put(getId() + H_KEY, String.valueOf(getSize().height));
preferences.put(getId() + MAX_KEY,
String.valueOf((getExtendedState() & JFrame.MAXIMIZED_BOTH) == JFrame.MAXIMIZED_BOTH));
try {
preferences.flush();
}
catch(BackingStoreException e) {
e.printStackTrace();
}
}
public void setSavedSize() {
Preferences preferences = Preferences.userNodeForPackage(this.getClass());
String xs = preferences.get(getId() + X_KEY, "");
String ys = preferences.get(getId() + Y_KEY, "");
String ws = preferences.get(getId() + W_KEY, "");
String hs = preferences.get(getId() + H_KEY, "");
String max = preferences.get(getId() + MAX_KEY, "");
if(max != null && !max.trim().isEmpty() && Boolean.valueOf(max) == true) {
setDefaultSize();
setExtendedState(JFrame.MAXIMIZED_BOTH);
return;
}
if(xs.length() == 0 || ys.length() == 0 || ws.length() == 0 || hs.length() == 0) {
setDefaultSize();
}
else {
sizeFromPreferences = true;
int x = Integer.parseInt(xs);
int y = Integer.parseInt(ys);
int w = Integer.parseInt(ws);
int h = Integer.parseInt(hs);
setLocation(x, y);
setSize(w, h);
}
}
编辑
如果要创建某种设置存储系统,可以使用与 RememberableFrame
中相同的约定 - 使用前缀。
If you want to create some kind of settings storing system you can use the same convention as in RememberableFrame
- use prefixes.
在 RememberableFrame
我使用:
private String getId() {
return this.getClass().getSimpleName() + id;
}
其中 id
是开发人员提供的自定义字符串或空字符串。但请记住,您要设置的属性的密钥有长度限制。
where id
is custom String provided by the developer or an empty string. But remember that the key of the property you want to set has length limit.
来自API:
static int MAX_KEY_LENGTH
Maximum length of string allowed as a key (80 characters).
static int MAX_NAME_LENGTH
Maximum length of a node name (80 characters).
static int MAX_VALUE_LENGTH
Maximum length of string allowed as a value (8192 characters).
您可能还会考虑专门用于存储设置目的。例如,类 KeyboardSettings
,甚至在不同的包中。
You might also consider using classes specifically for "storing settings" purposes. For example class KeyboardSettings
and even in different packages.
这篇关于从JAR中保存首选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!