java界面多语言切换

前言

使用ResourceBundleproperties文件完成中英文切换
在src下创建文件 language_en.properties
properites文件格式为ISO-8859-1,可能会出现部分字符异常,找原因中…
由于properties文件格式为ISO-8859-1,会出现乱码,需要使用native2ascii.exe将中文字符转换为指定编码,native2ascii在jdk的bin目录下。除此之外,需要将properties格式改为UTF-8
参考

https://blog.csdn.net/hengji666/article/details/80267929
https://blog.csdn.net/albertfly/article/details/51330512

代码

//定义
ResourceBundle rs = ResourceBundle.getBundle("language");
//定义下拉框
//主要看rs = ResourceBundle.getBundle("language", Locale.CHINA);
JComboBox comboBoxLanguage = new JComboBox();
comboBoxLanguage.setBounds(494, 262, 74, 21);
contentPane.add(comboBoxLanguage);
comboBoxLanguage.addItem("English");
comboBoxLanguage.addItem("Chinese");
comboBoxLanguage.addActionListener(new ActionListener() {
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		int index = comboBoxLanguage.getSelectedIndex();
		if (index == 1) {
			rs = ResourceBundle.getBundle("language", Locale.CHINA);
			updateLabel();
		} else {
			rs = ResourceBundle.getBundle("language", Locale.ENGLISH);
			updateLabel();
		}
	}
});

updateLabel函数

private static void updateLabel() {
	// AddDevice
	AddDevice.lblLoginDevice.setText(rs.getString("strLoginDevice"));
	AddDevice.lblDeviceAddress.setText(rs.getString("strDeviceAddress"));
	AddDevice.lblUserName.setText(rs.getString("strUserName"));
	AddDevice.lblPort.setText(rs.getString("strPort"));
	AddDevice.lblPassword.setText(rs.getString("strPassword"));
	AddDevice.btnAdd.setText(rs.getString("strAdd"));
	AddDevice.btnCancel.setText(rs.getString("strCancel"));
}

properties文件

#English
#AddDevice
#ID = value的形式
strDeviceAddress=Device Address
strUserName=User Name
strPort=Port
strPassword=Password
strAdd=Add
strCancel=Cancel
strLoginDevice=Login Device

参考

https://blog.csdn.net/zqx20032009/article/details/72629270

你可能感兴趣的:(Java)