This section provides a tutorial example on how to create a label with the javax.swing.JLabel class.
Problem: I want to create a label with Chinese characters.
Solution: Not very hard to do. See the following sample program:
/**
* JLabelChinese.java
* Copyright (c) 2002 by Dr. Herong Yang, http://www.herongyang.com/
*/
import java.awt.*;
import javax.swing.*;
public class JLabelChinese {
public static void main(String[] a) {
JLabel l = new JLabel();
l.setFont(new Font("SimSun",Font.PLAIN, 12));
l.setText("Hello world! - \u7535\u8111\u4F60\u597D\uFF01");
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(l);
f.pack();
f.setVisible(true);
}
}
If you run this example, you will get:
Note: To run this program, you need to have SimSun font installed on your system.
To verify this, search for \winnt\fonts\simsun.ttc if you are using Windows system.
Sample programs listed in this section have been tested with JDK 1.3.1, 1.4.2, 1.5.0. and 1.6.0