JavaFx:FXMLLoader - 找不到资源

时间:2017-04-06 07:50:27

标签: java javafx-8 resourcebundle fxmlloader

我准备了自己的类Internationalization,当在属性文件中找不到密钥时,它会扩展类ResourceBundle以进行异常挂起。

public class Internationalization extends ResourceBundle{
    private ResourceBundle bundle;

    public Internationalization(ResourceBundle bundle) {
        this.bundle = bundle;
    }

    public String getFormattedString(String key, Object[] args) {
       if (bundle.containsKey(key)) {
            String value = bundle.getString(key);
            value = replaceSingleQuotes(value);
            MessageFormat mf = new MessageFormat(value);
            return mf.format(args);
       } else {
           return '!' + key + '!';
       }        
    }

    private static String replaceSingleQuotes(String template) {
        return template.replace("'", "''");
    }  

    @Override
    protected Object handleGetObject(String key) {
        if (bundle.containsKey(key)) {
            return bundle.getString(key);
        } else {
            return '!' + key + '!';
        }
    }

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

但FXMLLoader仍会抛出异常“资源”MissingKey“未找到”。当密钥不在属性文件中时。

Internationalization lngStr = new Internationalization(ResourceBundle.getBundle("language/lngStr"));
FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/LogIn.fxml"));
loader.setResources(lngStr);
Pane pane = loader.load();
scrMain.setContent((Node) pane);

你能帮我找一下这个问题吗?

0 个答案:

没有答案