3

我需要我的应用程序以编程方式设置所有敏感组件的语言环境,例如JTextFieldsJTextAreas. 我也有日期信息(月份写成一个词),这也是对语言环境敏感的。

我写了以下代码,但它似乎没有完成这项工作:

    public static void setLocale(java.awt.Container c /* main form */, Locale locale /* Locale.ENGLISH */) {

        Component[] components = c.getComponents();

        for (Component comp : components) {
            if (comp instanceof java.awt.Container)
                setLocale((java.awt.Container) comp, locale);
            comp.setLocale(locale);    
        }
    }

代码有什么问题?

4

2 回答 2

0

以下代码应该可以解决问题:

public void switchDefaultLocale(Locale l) {
    if (! l.equals(Locale.getDefault())) {
        Locale.setDefault(l);
        JComponent.setDefaultLocale(l);
    }
}

但这只会对 JComponent 的新实例产生影响。如果要更新现有实例,请不要忘记在每个实例上调用 updateUI()。

于 2011-08-04T14:16:45.843 回答
0

基本上,我认为没有理由更改组件树中所有组件的语言环境。由于方法 getLocale() 自动搜索其父项。

/**
 * Gets the locale of this component.
 * @return this component's locale; if this component does not
 *          have a locale, the locale of its parent is returned
 */
public Locale getLocale();

所以设置树根的语言环境就足够了。但该地区是否在某处受到尊重,我现在无法确定。

再见

于 2015-05-16T13:24:05.113 回答