Setting unicode characters in java frames

喜夏-厌秋 提交于 2019-11-28 05:24:53

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/340739/setting-unicode-characters-in-java-frames

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!