设置组件区域设置不成功

发布于 2024-11-28 04:25:12 字数 543 浏览 0 评论 0原文

我需要我的应用程序以编程方式设置所有敏感组件的区域设置,例如 JTextFields 和 JTextAreas。我还有日期信息(月份写成一个单词),它也是区域设置敏感的。

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

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

代码有什么问题?

I need my application to set programmatically a locale of all the sensitive components, like JTextFields and JTextAreas. Also I have date information (month written as a word) which is locale-sensitive too.

I wrote the following code, but it doesn't seem to do the job:

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

What's wrong with the code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无声情话 2024-12-05 04:25:12

下面的代码应该可以解决这个问题:

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

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

The following code should do the trick:

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

But this will have an effect only on new instance of JComponent.
If you want to update existing instances, don't forget to call updateUI() on each.

呆° 2024-12-05 04:25:12

基本上,我不认为有理由更改区域设置
组件树中的所有组件。 由于方法
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();

所以设置区域设置就足够了
树的根。但是否尊重语言环境
某个地方,我现在还不能说。

再见

Basically, I don't see a reason to change the locale of
all components in the component tree. Since the method
getLocale() automatically searches its parents.

/**
 * 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();

So it should be enough to set the locale of the
root of the tree. But whether the locale is respected
somewhere, I can't tell right now.

Bye

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文