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
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值