Java Swing Tutorials - Herong's Tutorial Examples - v4.32, by Herong Yang
Displaying Chinese Characters in Frame Title
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 1: If you are using JDK 9 or higher on newer computers, you can include Chinese characters in your Java source code and save it in UTF-8 encoding, which is the default behavior of most programming editors. There is no need to specify the "SimSun" font name. The system will will pick up the default font to support Chinese characters. Chinese characters can still be provided in Unicode /uxxxx ASCII format. But UTF-8 encoding is a much better option.
/* JFrameChinese2.java
* Copyright (c) 2014-2024 HerongYang.com All Rights Reserved.
*/
import java.awt.*;
import javax.swing.*;
public class JFrameChinese2 {
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.setTitle("Hello world! - 电脑你好!");
f.setBounds(100,50,500,300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
Solution 2: If you are still using an older version of JDK on an older computer, you may have to install Chinese fonts yourself. In my sample code listed below, I am using font SimSun, which was installed as part of the Windows multi-language support. You may also have to enter Chinese characters in Java source code in /uxxxx ASCII format.
/* JFrameChinese.java * Copyright (c) 2014-2024 HerongYang.com All Rights Reserved. */ 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 both examples, you will get the same result:
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 on your Windows computer, you can look for %windir%\fonts\simsun.ttc file in your system directory tree.
Table of Contents
Introduction of Java Swing Package
Graphics Environment of the Local System
Creating Frames with Sizes and Locations
Closing Frame and Terminating Application
Listing and Interrupting AWT Threads
"AWT blocker activation interrupted" Error in JDK 1.6
JFrame Thread Behavior with JDK 8 to 20
►Displaying Chinese Characters in Frame Title
Drawing Graphics - Using paint() on Frame
Drawing Graphics - Using paint() on Component
Drawing Graphics - Using paint() on Content Pane
Drawing Chinese Characters on Frames
JCheckBox - Swing Check Box Class
JRadioButton - Swing Radio Button Class
JTextField - Swing Text Field Class
JComboBox - Swing Combo Box Class
Menu Bar, Menus, Menu Items and Listeners
Creating Internal Frames inside the Main Frame
Layout of Components in a Container
JEditorPane - The Editor Pane Class
SwingWorker - The Background Task Worker
AWT (Abstract Windows Toolkit)