gpt4 book ai didi

java jlabel setText(str) 当str有韩文字符时错误

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:20:38 27 4
gpt4 key购买 nike

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

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

  • J标签
  • JTextArea


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 有问题。

最佳答案

@mKorbel 很容易确定问题出在 JLabel 字体上。

在应用程序启动时从 Locale.getDefault() 识别语言或要求用户选择。然后根据选择的语言生成pick .properties文件的路径。

在我放入的韩语文件中(我使用 Eclipse AnyEdit 插件) 游泳=\u0412\u043e\u0434\u043d\u043e\u0435 运行=\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);

关于java jlabel setText(str) 当str有韩文字符时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12912283/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com