Java: Ví dụ HelloWorldSwing
/*
* HelloWorldSwing.java không yêu cầu file ngoài.
*/
import javax.swing.*;
public class HelloWorldSwing {
/**
* Tạo GUI và hiển thị nó. Để thread được an toàn,
* phương thức này phải được gọi từ
* thread chỉ định sự kiện.
*/
private static void createAndShowGUI() {
//Tạo và thiết lập cửa sổ.
JFrame frame = new JFrame("HelloWorldSwing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Thêm nhãn "Hello World".
JLabel label = new JLabel("Hello World");
frame.getContentPane().add(label);
//Hiển thị cửa sổ.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Lập lịch công việc cho luồng chỉ định sự kiện:
//tạo và hiển thị GUI của ứng dụng này.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}