Is it possible to write a GUI in Java that would display Japanese fonts correctly regardless of the language settings of the OS it's being run on?
I'd like to wri开发者_JS百科te a program which is able to do this but I'm not sure how to start going about this. Any advice would be a huge help!
The language settings aren't the problem here. Fonts are.
Java certainly can handle Japanese text, no matter what the system settings are, if the developer doesn't fall into the trap of depending on the platform default encoding by using things like FileReader
, or new String(byte[])
.
But to actually display the Japanese text, you need a font that has Japanese characters, and fonts are not part of Java. Depending on the OS, Japanese-capable fonts may be part of the default installation, or the user may be prompted to install them as needed, or they may have to be installed manually.
Java can easily display Japanese regardless of if the OS has the fonts installed or not, but this is only for Swing applications. Anything using the console window requires fonts installed in the OS.
Steps:
1) Download one of the truetype fonts from here : http://www.wazu.jp/gallery/Fonts_Japanese2.html
2) Use the following code to allow your swing clients to use your fonts:
InputStream fontStream = getClass().getResourceAsStream("/locationoffontonclasspath/myfontname.ttf");
Font japaneseEnabledFont = null;
boolean japaneseDisplayEnabled = false;
try {
japaneseEnabledFont = Font.createFont(Font.TRUETYPE_FONT, fontStream);
GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(japaneseEnabledFont);
japaneseDisplayEnabled = true;
} catch (Exception e) {
// handle exceptions here
} finally {
if (fontStream != null) {
try {fontStream.close();} catch (Exception e1) {}
}
}
if (japaneseDisplayEnabled) {
.....
}
Also, if you wish to use Japanese literals in your sourcecode you have to compile with -Dfile.encoding=utf-8. If using an IDE to compile then you can change the settings on the following screen (right click the project and select properties to get this window): screenshot
More information is available at this page
You can package a TrueType font with your program and load and use it in Swing components like so:
// Create a label in Japanese.
String message = "かつ、尊厳と権利とについて平等である。";
JLabel label = new JLabel(message);
// Load the TrueType font with Japanese characters and apply it.
File file = new File("msmincho.ttf");
Font font = Font.createFont(Font.TRUETYPE_FONT, file);
font = font.deriveFont(Font.PLAIN, 14f);
label.setFont(font);
Be careful to use the correct charset. If compiling Japanese characters in the code you should compile with javac -encoding utf-8 Foo.java
. Also be sure to use the charset explicitly when using Readers.
Add -Dfile.encoding=utf-8
to your command line script that's launching the application.
You can copy your truetype Japanese fonts in $JAVA_HOME\jre\lib\fonts\fallback. You'll have to create this folder. On linux a symbolic link will do the trick.
Source: http://www.asahi-net.or.jp/~ik2r-myr/kanji/german/linos_g.htm
精彩评论