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

标签 java swing properties resourcebundle externalizing

我可以使用属性文件和 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/

相关文章:

c# - 在 Viewmodel 属性中防止 StackOverFlow 或递归

Java 属性 : how to escape # (hash)

java - 从 Grails 中的 java 类读取属性文件

java - Android Sqlite 查询返回什么?

java - 如何让java在Mac上运行终端命令? (回显命令)

java - 在 Java Swing 中播放视频

java - 如何使子组件只处理它可以处理的事件,而使其他事件保持不变?

java - 如何在 HttpResponse 中发送多个内存工作簿

java - 对字段的非限定访问 - this

java - 根据数据库查询返回的字符串,为 JTable 列中的所有单元格设置不同的背景颜色