SwingGUI 工具包
Swing 是一個(gè)為Java設(shè)計(jì)的GUI工具包。 Swing 是 JAVA基礎(chǔ)類 的一部分。 Swing 包括了圖形用戶界面 (GUI) 器件 如:文本框,按鈕,分隔窗格和表。
SWING 提供許多比AWT更好的屏幕顯示元素。它們用純Java寫成,所以同Java本身一樣可以跨平臺(tái)運(yùn)行,這 一點(diǎn)不像AWT。 它們是JFC的一部分。 它們支持可更換的面板和主題(各種操作系統(tǒng)默認(rèn)的特有主題),然而不是真的使用原生平臺(tái)提供的設(shè)備,而是僅僅在表面上模仿它們。這意味著你可以在任意平臺(tái) 上使用JAVA支持的任意面板。 輕量級(jí)元件的缺點(diǎn)則是執(zhí)行速度較慢,優(yōu)點(diǎn)就是可以在所有平臺(tái)上采用統(tǒng)一的行為。
Swing程序外觀
示例代碼:
import javax.swing.*;
public class HelloWorldSwing {
/**
* 創(chuàng)建并顯示GUI。 出于線程安全的考慮,
* 這個(gè)方法在事件調(diào)用線程中調(diào)用。
*/
private static void createAndShowGUI() {
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);
//Create and set up the window.
JFrame frame = new JFrame("HelloWorldSwing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add the ubiquitous "Hello World" label.
JLabel label = new JLabel("Hello World");
frame.getContentPane().add(label);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}評(píng)論
圖片
表情
