gpt4 book ai didi

java - 如何使用 ResoucesBundles 在属性文件中存储与可用性相关的值(字体、尺寸、颜色...)

转载 作者:行者123 更新时间:2023-11-30 04:38:04 25 4
gpt4 key购买 nike

我可以使用属性文件和 ResourceBundle 类使用 ResourceBundle.getString() 获取字符串。甚至还可以使用以下方法获取 int 和 float 对象:

int a = (int) ResourceBundle.getObject("IntKey");
float b = (float) ResourceBundle.getObject("FloatKey");

但是我想知道如何获取一些复杂的对象,例如字体?

Font font = (Font) ResourceBundle.getObject("FontKey"); 

但是如何将字体值存储在属性文件中?我可以将对象存储为: new Font("Tahoma", Font.PLAIN, 12); 到属性文件的 key:value 中。

更新 1:

@doublesharp你的答案很好。实际上,我没有扩展 ResourceBundle 类来重写 handleGetObjects() 方法。我的实现如下图:

public class Usability {

private static final String BUNDLE_NAME = "com.upgrade.utility.resources.Usability";

private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME);

private Usability() {}

public static String get(String key, Object ... args) {

String value = null;
try {
value = RESOURCE_BUNDLE.getString(key);

for (Object var : args) {
if (var != null) {
try {
value = value.replaceFirst("@", var.toString());
} catch (Exception e) {}
}
}
} catch (MissingResourceException e) {
value = '!' + key + '!';
}
return value;
}

public static Font getFont(String key){
Font value = null;
try {
String fontName = (String) RESOURCE_BUNDLE.getString(key+ ".Name");
Integer fontStyle = Integer.parseInt(RESOURCE_BUNDLE.getString(key+ ".Style"));
Integer fontSize = Integer.parseInt(RESOURCE_BUNDLE.getString(key+ ".Size"));
value = new Font(fontName, fontStyle, fontSize);
} catch (MissingResourceException e) {
value = new Font("Tahoma", Font.PLAIN, 11);
}catch (NullPointerException npe) {
value = new Font("Tahoma", Font.PLAIN, 11);
}

System.out.println("Font"+ value);
return value;

}
}

在这种情况下我该如何使用你的方法?我是 JAVA 新手,您能告诉我如何修改我的实现以使用方法 handleGetObjects() 吗?

更新 2:

@doublesharp:根据您的上一条评论,我已经进行了这样的修改,但是在可用性类的第三行中出现了 Class Cast 异常。

public class Usability {

private static final String BUNDLE_NAME = "com.upgrade.utility.resources.Usability";

public static final MyResourceBundle RESOURCE_BUNDLE = (MyResourceBundle) MyResourceBundle.getBundle(BUNDLE_NAME);

private Usability() {}

public static String get(String key, Object ... args) {

String value = null;
try {
value = RESOURCE_BUNDLE.getString(key);

for (Object var : args) {
if (var != null) {
try {
value = value.replaceFirst("@", var.toString());
} catch (Exception e) {}
}
}
} catch (MissingResourceException e) {
value = '!' + key + '!';
}
return value;
}

}

我的扩展 ResourceBunlde 类是:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.util.Enumeration;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

public class MyResourceBundle extends ResourceBundle{

@Override
public Object handleGetObject(String key) {
if (key.contains("Font")) {
return getFont(key);
} else if (key.contains("color")){
return getColor(key);
}else if (key.contains("Dimension")){
return getDimension(key);
}
return this.getObject(key);
}

public Font getFont(String key){
Font value = null;
try {
String fontName = (String) this.getString(key+ ".Name");
Integer fontStyle = Integer.parseInt(this.getString(key+ ".Style"));
Integer fontSize = Integer.parseInt(this.getString(key+ ".Size"));
value = new Font(fontName, fontStyle, fontSize);
} catch (MissingResourceException e) {
value = new Font("Tahoma", Font.PLAIN, 11);
}catch (NullPointerException npe) {
value = new Font("Tahoma", Font.PLAIN, 11);
}
return value;
}

public Color getColor(String key){
Color value = null;
try {
Integer R = Integer.parseInt(this.getString(key+ ".R"));
Integer G = Integer.parseInt(this.getString(key+ ".G"));
Integer B = Integer.parseInt(this.getString(key+ ".B"));
value = new Color(R, G, B);
} catch (MissingResourceException e) {
// value = new Color("Tahoma", Font.PLAIN, 11);
}catch (NullPointerException npe) {
// value = new Color("Tahoma", Font.PLAIN, 11);
}
return value;
}

public Dimension getDimension(String key){
Dimension value = null;
try {
Integer X = Integer.parseInt(this.getString(key+ ".X"));
Integer Y = Integer.parseInt(this.getString(key+ ".Y"));
value = new Dimension(X, Y);
} catch (MissingResourceException e) {
// value = new Color("Tahoma", Font.PLAIN, 11);
}catch (NullPointerException npe) {
// value = new Color("Tahoma", Font.PLAIN, 11);
}
return value;
}

@Override
public Enumeration<String> getKeys() {
return null;
}

}

如何解决这个异常?

我的回答有问题吗?使用我刚才打电话的方式

Usability.getFont("JPanelUpgradeTypeScreen.ElementLabelFont");

但是使用你的答案技术,我需要这样调用它(调用中需要类型转换):

(Font)Usability.RESOURCE_BUNDLE.handleGetObject("JPanelUpgradeTypeScreen.ElementLabelFont");

最佳答案

不应在属性文件中初始化对象。您应该在属性文件中仅使用常量值

关于java - 如何使用 ResoucesBundles 在属性文件中存储与可用性相关的值(字体、尺寸、颜色...),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12947661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com