我嘗試在運行時更改我的swing應用程序中的語言環境。
但我不知道它應該如何工作,或者沒有總體規劃?如何在java swing中運行時更改語言
我只能想到兩個選擇:
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")); // } }); } }
[永遠不需要,但必須有索姆e有關將屬性加載到HashMap或List的示例](http://java2s.com/Tutorial/Java/0220__I18N/Catalog0220__I18N.htm) – mKorbel