问题描述
我确信有很多方法,但推荐的方法是什么,对您的代码影响最小?
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...
推荐答案
我想出的唯一解决方案是创建一个包含所有需要重新渲染的组件的大型注册表.然后,如果调用切换区域设置,您只需调用注册表,它将遍历所有已注册的组件并调整它们的值.因此,例如,对于所有已注册的 JLabels,它会按照以下方式做一些事情:
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 中的位置.然后无论当前正在渲染什么屏幕,主父面板都负责重新渲染它.同样通过这种方式,注册表不必管理 Locale 键,这些键与注册表完全分离.每个组件负责管理它自己的 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 桌面应用程序国际化的最佳实践是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!