Nik*_*sov 5 java swing jlabel utf-8 internationalization
我正在开发支持英语、俄语和韩语的 Java 应用程序。
所以我为每种语言准备了 unicode 属性文件。然后我使用捆绑包中的 _ 函数获取一些字符串值,将其设置为
InputStream stream = LocaleManager.class.getClassLoader().getResourceAsStream(path);
ResourceBundle bundle = new PropertyResourceBundle(new InputStreamReader(stream, "UTF-8"));
public static String _(String key) {
return bundle.getString(key);
}
Run Code Online (Sandbox Code Playgroud)
对于英语和俄语来说,它非常有效。对于韩语,JTextArea 可以正确显示韩语字符,但 JLabel 不能。它显示方块,在 Eclipse 控制台中显示 ??,但是俄罗斯字符可以在 Eclipse 控制台中正确显示。
所以看起来 JLabel 有问题。
@mKorbel 很容易发现问题出在 JLabel 字体上。
应用程序启动时从 Locale.getDefault() 识别语言或要求用户选择。然后根据选择的语言生成选择.properties文件的路径。
在韩语文件中,我放入(我使用 Eclipse AnyEdit 插件) Swimming=\u0412\u043e\u0434\u043d\u043e\u0435 Running=\u0411\u044b\u0441\u0442\u0440\u043e\u0435
InputStream stream = LocaleManager.class.getClassLoader().getResourceAsStream(path);
ResourceBundle bundle = new PropertyResourceBundle(new InputStreamReader(stream, "UTF-8"));
//get internationalized version for "Swimming"
String str = _("Swimming");
//create and configure JLabel
JLabel label = new JLabel();
label.setVisible(true);
label.setBackground(Color.yellow);
label.setOpaque(true);
//this line was the issue
label.setFont(new Font("Verdana", Font.PLAIN, 14));
//setting text which results in squares
label.setText(str);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2104 次 |
| 最近记录: |