This section provides a tutorial example on how to display Chinese characters in the title bar of a frame window with a given Chinese font.
Problem: I want to display Chinese characters in the title of a frame
window.
Solution: You can only do this if you are using JDK 1.4.2 and have a
Unicode font installed to support Chinese characters. In my sample code listed
below, I am using font SimSun, which was installed as part of Windows
multi-language support.
/**
* JFrameChinese.java
* Copyright (c) 2004 by Dr. Herong Yang
*/
import java.awt.*;
import javax.swing.*;
public class JFrameChinese {
public static void main(String[] a) {
JFrame f = new JFrame();
f.setFont(new Font("SimSun",Font.PLAIN, 12));
f.setTitle("Hello world! - \u7535\u8111\u4F60\u597D\uFF01");
f.setBounds(100,50,500,300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
If you run this example, you will get:
Note 1: With JDK 1.4.1 or lower, the characters are displayed as "?".
I don't any way to fix it. If you have any suggestions, please share them
with me.
Note 2: To check if you have SimSun font installed or not, you can look
for \winnt\fonts\simsun.ttc file in your system directory tree.
Sample programs listed in this section have been tested with JDK 1.4.2, 1.5.0. and 1.6.0