在java框架中设置unicode字符 [英] Setting unicode characters in java frames

查看:363
本文介绍了在java框架中设置unicode字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在没有日语语言包的Windows XP m / c中以Java swing的JFrame标题显示unicode字符(例如japanese)?看起来像将标题文本设置为日语unicode字符,而字体为MS Mincho是不够的。虽然这是在Swing标签中显示unicode字符所需要做的全部吗?

解决方案

没有日语语言包?



看来你必须至少 ,包括 字体配置文件



从Java5及更高版本开始,您不再需要font.properties文件,因为您可以加载字体文件以创建/使用字体。

  String fontFileName =yourfont.ttf; 
InputStream = this.getClass()。getResourceAsStream(fontFileName);
字体ttfBase = Font.createFont(Font.TRUETYPE_FONT,is);
字体ttfReal = ttfBase.deriveFont(Font.PLAIN,24);


How to display unicode characters (e.g. japanese) in the title of a JFrame in java swing in a Windows XP m/c without the Japanese language pack? It looks like setting the title text to Japanese unicode characters and the font to MS Mincho is not enough. While this is all you need to do to display unicode characters in Swing labels?

解决方案

"without the Japanese language pack" ?

It seems you have to at least download the language font...

The font is the only thing that needs to be installed on your client machine to run the application.

Using the font is lots easier in Swing unlike in AWT.
For AWT components i.e one that has a native peer, you need to customize the settings of the JRE i.e modify font.properties under /jre/lib to include the font you have installed under each font type.

In your Swing application, you just need to set the font of the Swing component before setting its text.

The link at the beginning of the post contains a complete example.
Small extract:

JFrame frame = new JFrame();
String string = "\u30b7\u30f3\u30d7\u30eb\u30c6\u30ad\u30b9\u30c8\u30a8\u30c7\u30a3\u30bf";
JLabel label = new JLabel();
label.setFont(new Font("MS Mincho",Font.PLAIN, 12));
label.setText(string);
frame.getContentPane().add(label);
frame.setFont(new Font("MS Mincho",Font.PLAIN, 12));
frame.setTitle(string);


The general documentation for java J2SE6 (1.6.0) is here, included the Font Configuration Files

From Java5 and later, you do not need font.properties file anymore, since you can load a font file in order to create/use a font.

String fontFileName = "yourfont.ttf";
InputStream is = this.getClass().getResourceAsStream(fontFileName);
Font ttfBase = Font.createFont(Font.TRUETYPE_FONT, is);
Font ttfReal = ttfBase.deriveFont(Font.PLAIN, 24);

这篇关于在java框架中设置unicode字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆