我可以使用属性文件和 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);
到属性文件的键:值中。
更新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:从您的上一条评论中,我已经进行了这样的修改,但是在可用性类的第 3 行中出现了 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");