1

我可以使用属性文件和 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");
4

5 回答 5

2

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

于 2012-10-18T05:29:20.460 回答
0

您需要handleGetObject()为您的实现覆盖该方法。您不能将对象定义存储在属性文件中,但您可以将其初始化参数存储为键,并在返回对象时使用它们。在此示例中,您将为font-family(Tahoma 等)、font-type(用于映射到 Font.BOLD 等的文本值)和font-size. 调用MyResourceBundle.getObject("font")(一个扩展的类ResourceBundle)会调用你的实现,handleGetObject()然后使用之前的值来创建你的对象:

@Override
public Object handleGetObject(String key) {
   if (key.equals("font")) {
      String family = this.getString("font-family");
      String strType = this.getString("font-type");
      int type;
      switch (strType){
          case "bold": Font.BOLD;
          default: type = Font.PLAIN;
      }
      int size = this.getInt("font-size");
      return new Font(family, type, size);
   } else {
      return null;
   }
}
于 2012-10-18T05:50:08.263 回答
0

您可以序列化您的自定义对象,然后使用 base64 编码包装二进制代码,例如,您可以将其作为字符串写入属性

于 2012-10-18T05:52:25.363 回答
0

从您的所有答案和评论中获取输入:这是答案,满足我的要求:

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

在我存储为的属性文件中

#Usability
    DialogTextFont.Name=Tahoma
    DialogTextFont.Style=0
    DialogTextFont.Size=12
于 2012-10-18T06:28:33.340 回答
0

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

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

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

#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
于 2013-01-06T03:24:20.090 回答