4

我正在开发应该支持英语、俄语和韩语的 Java 应用程序。

所以我为每种语言准备了 unicode 的属性文件。然后我使用 bundle 中的 _ 函数获取一些字符串值,将其设置为

  • JLabel
  • 文本区域


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);
}

对于英语和俄语来说,它完美无缺。对于韩语 JTextArea 正确显示韩语字符,但 JLabel 不正确。它显示正方形,在 Eclipse 控制台中显示 ??,但是俄罗斯字符可以在 Eclipse 控制台中正确显示。

所以 JLabel 似乎有问题。

4

1 回答 1

4

由于@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);
于 2012-10-16T10:57:21.773 回答