第一個示例
在本頁中,我們將開始使用 swing。我們將看看 `JFrame` 和 `JLabel` 類,並構建一個基本的 swing “hello world” 應用程式。
JFrames 是 swing 元件。swing 元件是圖形使用者介面 (GUI) 的一部分。框架、視窗、文字框、按鈕、開關以及 GUI 應用程式的許多其他部分都是元件。所有 swing 元件的根類是 `JComponent` 類,其他 swing 元件,包括 `JFrame`,都是 `JComponent` 的子類。`JComponent` 是一個抽象類,因此不能直接例項化,但可以進行子類化,這在您想要編寫自己的自定義 GUI 元件時很有用。
JFrame 是一個頂級元件,這意味著它包含其他元件,但自身並不被包含。它的螢幕外觀由平臺決定,但通常它是一個視窗,使用者可以調整大小、移動、最大化、最小化,並且有自己的標題欄。
可能最熟悉 `JFrame` 以及元件本身的方法是執行一個建立 `JFrame` 的簡短示例程式。以下程式將完成此任務。
一個簡單的 Swing 應用程式
import javax.swing.JFrame; // All swing components live
// in the javax.swing package
// A simple class to test a JFrame
public class JFrameTest {
public static void main(String[] args) {
// create the frame. The constructor optionally takes
// a string as an argument that's used as the title.
JFrame frame = new JFrame("This is a test!");
// set the size -- 300 by 300 is good
frame.setSize(300, 300);
// this next line is currently commented. If you
// uncomment it, the user will not be able to resize
// the JFrame
// frame.setResizable(false);
// has the application exit when the frame is closed
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// makes the frame visible. The frame is invisible
// until this method is called.
frame.setVisible(true);
// these lines are also commented, but you can uncomment
// them to see their effects. The first will center the
// JFrame in the center of the screen. The second will
// make it the front, focused window.
// frame.setLocationRelativeTo(null);
// frame.toFront();
}
}
|
程式碼建立了一個空的 JFrame。當您執行它時,您應該會看到一個窗口出現在您的螢幕上。程式碼還說明了一些可以與 JFrames 一起使用的方法,其中一些已註釋掉。我鼓勵您嘗試一下注釋和取消註釋這些方法,看看效果。
一個空的框架並沒有那麼令人興奮,所以在這一節中,我們將看看 JLabels,並將一個放在 JFrame 中。`JLabel` 是一個 GUI 元件,可以容納影像、一些文字或兩者。
以下程式碼建立了一個 GUI 版本的 “Hello World!” 應用程式。
Swing 中的 hello world
import javax.swing.*; // All swing components live
// in the javax.swing package
// A GUI hello world application
public class Hello {
public static void main(String[] args) {
// creates the label. The JLabel constructor
// takes an optional argument which sets the text
// of the label.
JLabel label = new JLabel("Hello, World!");
// The next line is commented out. If it is
// uncommented, the text will be aligned with
// the center of the frame, otherwise it will
// align on the left.
// label.setHorizontalAlignment(SwingConstants.CENTER);
JFrame frame = new JFrame("Hello");
frame.add(label);
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.toFront();
}
}
|
這個程式與前一個程式非常相似,只是它除了 `JFrame` 之外還建立了一個帶有文字 “Hello, World!” 的 `JLabel`,並使用 `JFrame` 的 `add()` 方法將它新增到框架中,然後使它可見。如果您按照原樣編譯並執行程式,那麼 “Hello, world” 文字將對齊在視窗的左側。
透過取消註釋 `label.setHorizontalAlignment(SwingConstants.CENTER);` 行,您可以將文字移動到中心。
我們已經看到了如何建立一個 JFrame,它是一個 swing GUI 應用程式的頂級容器,以及一些可以用來更改其設定的 `JFrame` 方法。我們還查看了 `JLabel` 類,並使用 `JFrame` 的 `add()` 方法將 `JLabel` 新增到框架中,以建立一個簡單的應用程式。
透過檢視這些示例,您可能想知道 JFrames、JLabels 和其他元件還有哪些其他方法。儘管我們將繼續檢視更多元件和更多方法。幸運的是,Oracle 為所有核心 Java 類(包括 swing)提供了一個寶貴的線上參考,可以在以下連結中檢視
http://docs.oracle.com/javase/7/docs/api/index.html
