如何使用 ResoucesBundles 在属性文件中存储与可用性相关的值(字体、尺寸、颜色...) [英] how to store Usability related values (Font, Dimension, Color,...) in properties file using ResoucesBundles

查看:64
本文介绍了如何使用 ResoucesBundles 在属性文件中存储与可用性相关的值(字体、尺寸、颜色...)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm able to fetch the Strings using a properties file and ResourceBundle class using ResourceBundle.getString(). And even able to fetch int and float objects also using:

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

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

But I want to know to how to fetch some complex objects like fonts?

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

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

But how to store the Font values in properties file? can I store the object like: new Font("Tahoma", Font.PLAIN, 12); into a key:value of a properties file.

@doublesharp 你的回答很好.实际上我没有扩展 ResourceBundle 类来覆盖 handleGetObjects() 方法.我的实现如下图所示:

@doublesharp your answer is fine. Actually I'm not extending ResourceBundle class to override handleGetObjects() method. My implemention is as shown below:

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() 方法?

How Can I use your method in this case? I'm new to JAVA, can you please tell me how to modify my implementation to use the method handleGetObjects() ?

@doublesharp:从你的最后一条评论来看,我已经这样修改了,但是在可用性类的第三行中出现了 Class Cast 异常.

@doublesharp: From your last comment, I've modified like this, But getting Class Cast exception in 3rd line of Usability class.

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 类是:

My extended ResourceBunlde class is:

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;
    }
    
}

如何解决这个异常?

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

And also Is there problem in my answer? using which I was just calling like

Usability.getFont("JPanelUpgradeTypeScreen.ElementLabelFont");

但是使用您的回答技巧,我需要这样称呼它(通话中需要类型转换):

But using your answer techinque, I need to call it like (Type conversion is needed in call) :

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

推荐答案

在一个类中,我定义了以下函数,每当我需要访问与可用性相关的值时,我都会调用这些函数.所有与可用性相关的值都存储在一个公共位置(属性文件).

In a class I've defined the following functions, which I call whenever I need to access the usability related valus. All the usability related values are stored at a common place (properties file).

private static final String BUNDLE_NAME = "com.testApp.resources.properties.Usability";

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

    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);
        }
        return value;
    }

    public static Color getColor(String key){
        Color value = null;
        try {
            Integer R = Integer.parseInt(RESOURCE_BUNDLE.getString(key+ ".R"));
            Integer G = Integer.parseInt(RESOURCE_BUNDLE.getString(key+ ".G"));
            Integer B = Integer.parseInt(RESOURCE_BUNDLE.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 static Dimension getDimension(String key){
        Dimension value = null;
        try {
            Integer X = Integer.parseInt(RESOURCE_BUNDLE.getString(key+ ".X"));
            Integer Y = Integer.parseInt(RESOURCE_BUNDLE.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;
    }
}

在我为可用性相关值维护的属性文件中,属性定义如下:

In properties file which I'm maintaining for usability related values the properties are defined as below:

#BLACK
ElementLabelFont.Color.R=4
ElementLabelFont.Color.G=4
ElementLabelFont.Color.B=4

#ScreenPanel dimension
ScreenPanel.Dimension.X=632
ScreenPanel.Dimension.Y=625

#Font of jCheckBoxYesAgreement
JPanelPreUpgradeStepsScreen.jCheckBoxYesAgreement.Font.Name=Tahoma
JPanelPreUpgradeStepsScreen.jCheckBoxYesAgreement.Font.Style=0
JPanelPreUpgradeStepsScreen.jCheckBoxYesAgreement.Font.Size=12

这篇关于如何使用 ResoucesBundles 在属性文件中存储与可用性相关的值(字体、尺寸、颜色...)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆