2012-10-16 32 views
3

我正在开发支持英语,俄语和韩语语言的Java应用程序。当str有韩文字符时,java jlabel setText(str)错误

因此,我已经准备好了各种语言的unicode属性文件。然后,我用_功能从包来设置它得到一些字符串值

  • 的JLabel
  • 的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的问题。

+3

可以请您演示您的问题,发布[SSCCE](http://sscce.org/),并请使用Unicode(“\ uxxxx”)而不是粘贴文本(Internet Explorer问题,可以使用各种编码页面设置),有两种方法,从属性文件(特别是在Windows平台上)编码的错误字符集或不支持韩文字符的字体,例如[SSCCE](http:// stackoverflow .com/a/12903751/714968) – mKorbel

+0

@mKorbel,y ou是正确的问题是与字体。我为jLabel设置了Verdana。我已经评论过该行,现在韩文字符显示正常。 –

+0

您可以发布答案,我会接受它。谢谢 –

回答

4

由于@mKorbel很容易识别出问题出在JLabel字体上。

在应用程序启动时从Locale.getDefault()标识语言或要求用户选择。 然后根据选择的语言生成选择.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); 
相关问题