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