我在swing应用程序中尝试在运行时更改Locale.
但我不知道它应该如何工作,还是没有总体规划?
但我不知道它应该如何工作,还是没有总体规划?
我只能想到两个选择:
1.重新启动应用程序,而不是最好的用户体验.
2.创建一个可以注册/注销组件的本地化管理器,在更改时,它将迭代所有组件并更改文本.
1和2都觉得尴尬.
其他信息:
目前,方向不是目标.
该应用程序被混淆.
例:
LocRes_en.properties:
text1 = English text
LocRes_ja.properties
text1 = Japanese text
ChangeLocale.java:
import java.awt.EventQueue; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Locale; import java.util.ResourceBundle; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; public class ChangeLocale { private JFrame frame; public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { ChangeLocale window = new ChangeLocale(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } public ChangeLocale() { initialize(); } private void initialize() { frame = new JFrame(); frame.setBounds(100,100,450,300); frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE); FlowLayout flowLayout = new FlowLayout(FlowLayout.CENTER,5,5); frame.getContentPane().setLayout(flowLayout); JButton btnChangeLoc = new JButton("Change Locale"); frame.getContentPane().add(btnChangeLoc); final JLabel lblLabel1 = new JLabel("New label"); frame.getContentPane().add(lblLabel1); Locale.setDefault(new Locale("en")); ResourceBundle r = ResourceBundle.getBundle("LocRes"); lblLabel1.setText(r.getString("text1")); btnChangeLoc.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Locale.setDefault(new Locale("ja")); ResourceBundle r = ResourceBundle.getBundle("LocRes"); // Manually iterate through all components :( lblLabel1.setText(r.getString("text1")); // } }); } }
解决方法
你可以尝试LocaleChangeListener接口 –