UI属性不包含某些键 [英] UI properties does not contain some keys

查看:104
本文介绍了UI属性不包含某些键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题。我需要获取UI属性:

I have the following problem. I need to get an UI properties:

UIManager.getString("OptionPane.okButtonText")

返回字符串OK,它可以正常工作。但是,如果我遍历 UIDefaults 键集,我永远不会得到键OptionPane.okButtonText。有谁知道它为什么会发生?我以三种不同的方式得到 UIDefaults UIManager.getDefaults() UIManager.getLookAndFeel ()。getDefaults() UIManager.getLookAndFeelDefaults()),但这些都没有。

that returns the string "OK", and it works. However, if I iterate through the UIDefaults keyset, I never get the key "OptionPane.okButtonText". Does anyone know why it happens? I get the UIDefaults in three different way (UIManager.getDefaults(), UIManager.getLookAndFeel().getDefaults() and UIManager.getLookAndFeelDefaults()), but no one of these work.

编辑:我还找到了这个 JFileChooser a>,包含一些不出现在 UIDefaults 键集中的属性。问题是:如何以编程方式获取所有这些属性?

Edit: I also find this list of properties of the class JFileChooser, that contains some properties that do not appear int the UIDefaults keyset. The problem is: how programmatically get all this properties?

编辑:代码示例:

UIDefaults defaults = UIManager.getDefaults();
String thekey = "OptionPane.okButtonText";
System.out.println(thekey + ": " + UIManager.getString(thekey));
for (Enumeration e = defaults.keys(); e.hasMoreElements();) {
    Object key = e.nextElement();
    System.out.println(key + ": " + defaults.get(key));
}

此代码返回打印这些属性。键OptionPane.okButtonText不会出现在输出中。

this code return print these properties. The key "OptionPane.okButtonText" dont appear in the output.

推荐答案

这可能是resourceBundles的一个问题:optionPane(以及fi fileChooser和其他)文本属性是从本地化的bundle加载的。 com.sun.swing.internal.plaf下的内部类是(过去不完全确定是否仍然如此)。也许那里出了问题...

This could be a problem with resourceBundles: the optionPane (as well as f.i. fileChooser and other) text properties are loaded from localized bundles. They are (used to be, not entirely sure if that's still the case) internal classes under com.sun.swing.internal.plaf. Maybe something's going wrong there ...

这里是workforme的片段:

here's the snippet that worksforme:

    String ok = "OptionPane.okButtonText";
    String text = ""; 
    text += " LAF: " + UIManager.getLookAndFeelDefaults().get(ok);
    text += " lookup: " + UIManager.get(ok);
    text += " default: " + UIManager.getDefaults().get(ok);
    System.out.println(text);

    // output, whereever I add that:
     LAF: OK lookup: OK default: OK

独立于当前安装的LAF。我的系统是win / vista,我的默认语言环境

independent of which LAF is currently installed. My system is win/vista, my default locale de

编辑:只是为了澄清 - 本地化资源不一定是keys()/ entrySet()中的直接条目,这些是Hashtable中的方法,它们在UIDefaults中没有被覆盖。因此,虽然我的代码片段中的查找应始终有效查询枚举是错误的 - 条目不在那里,但在一些由resourceBundles提供的缓存映射中。

just to clarify - the localized resources are not necessarily direct entries in the keys()/entrySet(), these are methods in Hashtable which are not overridden in UIDefaults. So while the lookup as in my snippet should always work querying the enums is wrong - the entries are not there but in some cached maps which are fed by resourceBundles.

编辑2:添加了确定的def(认为在谈了几个小时关于那个键之后会很明显:​​ - )

added the def of ok (thought that would be ... obvious after talking for several hours about that key :-)

Edit3:对于进一步的实验,我们应该在Locales中找到一个与OK不同的值,fi cancelButtonText

for further experiments, we should probably lookup a value which differs more than "OK" across Locales, f.i. cancelButtonText

编辑4(在重大突破之前的最后一次,承诺:-) - 关于如何找到所有本地化值是不可能的,如果不诉诸脏意味着(又名:实施细节)。我能想到的唯一方法是查看那些 - 假定 - 加载的resourceBundles,比如

Edit 4 (the very last before a major break, promised :-) - as to "how-to find all localized values" is not possible without resorting to dirty means (aka: implementation details). The only way I can think of is to look into the resourceBundles which are - assumedly - loaded, like

    import com.sun.swing.internal.plaf.basic.resources.basic;

    String cancel = "OptionPane.cancelButtonText";
    ListResourceBundle bundle = new basic();
    for (String key : bundle.keySet()) {
        if(cancel.equals(key)) {
            System.out.println(key
                    + ": " + bundle.getString(key));

        }
    }

这篇关于UI属性不包含某些键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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