本文介绍了国际化Java Swing桌面应用程序的最佳实践是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确信有很多方法,但建议的方法是什么,对代码的影响最小?

I'm sure there are a lot of methods, but what's the recommended way to do this that has the least amount of impact to your code?

显而易见的是,您创建了属性文件,但是如何在渲染中交换值?在J2EE中,您只需重新渲染整个页面,这样就很容易了。但是在Swing应用程序中,您只是在paintComponent(Graphics g)方法中添加.getProperty()的代码吗?

The obvious is that you create properties files, but how do you swap the values in the rendering? In J2EE you always just re-render the whole page so it's easy. But in Swing apps, do you just add the code for the .getProperty() in the paintComponent(Graphics g) method?

如果是这样的话,它看起来不是很重,因为现在你不得不在不需要之前的任何地方覆盖这个方法......

If that's the case, doesn't it seem heavy since now you'll have to override this method everywhere where before you didn't need to...

其他: 如何设置通知系统以重新呈现所有当前可见的组件而不强制进行某种注册模式?

Additional: How do you setup a notification system to re-render all currently visible components without forcing some kind of registration pattern?

我想如果我覆盖paintComponent(Graphics g),我所要做的就是触发一个事件已经改变了,paintComponent(Graphics g)方法将是叫做...

I guess if I overwrite paintComponent(Graphics g) all I have to do is fire an event that something has changed and the paintComponent(Graphics g) method will be called...

推荐答案

我想出的唯一解决方案是创建一个需要的所有组件的大规模注册表重新呈现。然后,如果调用切换Locale,您只需调用注册表,它将遍历所有已注册的组件并调整其值。因此,例如对于所有已注册的JLabel,它将执行

The only solution I came up with was to create a massive registry of all components that would need to be re-rendered. Then if a call is made to switch Locale you can just call the registry and it will go through all the registered components and adjust their values. So for example for all the registered JLabels it will do something along the lines of

for(JLabel specificJLabel : REGISTRY.registeredJLabels)
{
  String localeKey = specificJLabel.getActionCommand();
  specificJLabel.setText(ResourceBundle.getString(localeKey));
}

其中Locale键存储在组件ActionCommand中。然后,无论当前呈现什么屏幕,主父面板都负责重新呈现它。此外,注册表不必管理区域设置密钥,这些密钥与注册表完全分离。每个组件都负责管理它自己的ResourceBundle的Locale Keys。

where the Locale key is stored in the components ActionCommand. Then whatever screen is currently being rendered, the main parent panel is responsible for re-rendering it. Also this way the registry doesn't have to manage the Locale keys, these are completely decoupled from the registry. Each component is responsible to manage it's own Locale Keys to the ResourceBundle.

这篇关于国际化Java Swing桌面应用程序的最佳实践是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 09:48