package com.swtjface.Ch2;
import org.eclipse.jface.window.*;
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
public class HelloSWT_JFace extends ApplicationWindow {
public HelloSWT_JFace() {
super(null); //1.Window allocation
}
protected Control createContents(Composite parent) {
Text helloText = new Text(parent, SWT.CENTER);
helloText.setText("Hello SWT and JFace!");
parent.pack();
return parent; //2.Window presentation
/*處理窗口的設計,由于ApplicationWindow的可視部分不能被直接access,此方法連同一個Composite來控制GUI的顯示,此container對象[Composite]是所有被加入應用程序的GUI組件的父件*/
}
public static void main(String[] args) {
HelloSWT_JFace awin = new HelloSWT_JFace();
awin.setBlockOnOpen(true);
awin.open();
Display.getCurrent().dispose();? //3.Window operation
/*負責GUI的實際運作。在分派好ApplicationWindow的資源之后,main方法使窗口顯示,當setBlockOnOpen()方法以一個true參數被調用的時候,窗口關閉。然后ApplicationWindow的open()方法被調用,根據createContent()方法所返回的Composite來顯示窗口。然后程序通過dispose()方法釋放GUI的Display實例.因為此程序中的所有widget都是display的child,所以一旦釋放Display,所有的widget都被釋放*/
}
}
SWT將GUI的外觀和操作都放在它的Shell類里,而SWT/JFace卻將兩者分離開來了,其中外觀由createContents()方法內的Compsite來控制,而操作部分大體上是通過ApplicationWindow類的實例來實現的。
SWT/JFace同樣需要一個單獨的Display實例,但是只要ApplicationWindow通過一個null參數來構建,那么就會創建它自己的Shell。參閱下圖

ApplicationWindow在Shell的基礎上提供了更多的途徑來設計窗口,其相關方法如下:
addMenuBar()
Configures the window with a top-level menu
addToolBar()
Adds a toolbar beneath the main menu
addStatusLine()
Creates a status area at the bottom of the window
setStatus(String)
Displays a message in the status area
getSeparator()
Returns the line separating the menu from the window
setDefaultImage(Image)
Displays an image when the application has no shell
setExceptionHandler(IExceptionHandler)
Configures the application to handle exceptions according to the specified interface
package com.swtjface.Ch2;
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
public class HelloSWT {
public static void main (String [] args) {
Display display = new Display();
Shell shell = new Shell(display); //1.Allocation and initialization。
/*生成一個Display和Shell類的實例,GUI獲取底層平臺的資源并開辟了一個主窗口。*/
Text helloText = new Text(shell, SWT.CENTER);
helloText.setText("Hello SWT!");
helloText.pack();//2.Adding widgets to the shell
/*
在
Shell
上加入一個文本小部件。The code in this section also sets the parameters for these widgets, containers, and events to make sure they look and act as required.其中pack()方法是tell the Shell and Text components to use only as much space as they need.*/
shell.pack();
shell.open();
while (!shell.isDisposed()){
if (!display.readAndDispatch()) display.sleep();
}
display.dispose(); //3.GUI operation
/*一旦Shell的open()方法被調用,應用程序的主窗口和其子部件都會被呈現。只要Shell保持在打開狀態,Display實例就會通過它的readAndDispatch()方法來追蹤在操作系統事件隊列中的相關用戶事件。當窗口關閉時,與Display對象(包括Shell以及其子部件等)相聯系的資源就全部釋放*/
}
}
Display
類并不是可見的,但它負責監管著
GUI
的資源并管理著和操作系統的通信。它不光要關注著它自己的窗口是如何顯示、移動和重畫的,還同時要確保諸如鼠標點擊、鍵盤敲擊等事件送達widgets并去處理它們。
是任何
SWT
和
JFace
應用程序的承載著,無論你是用
SWT/JFace
開發或是單用
SWT
開發,你必須在你的程序中包含這個類的一個實例。
Display
類的主要任務就是負責將你的代碼重的
SWT
和
JFace
命令翻譯成底層的命令來調取操作系統。
這一過程包含了兩部分:
1.Display
對象構建一個代表著操作系統平臺的
OS
類的實例;這個類通過一系列被稱之為native methods的特殊
Java
程序提供了接觸計算機底層資源的途徑。2.
這個
Display
對象使用這些方法來直接指令操作系統并向應用程序傳達用戶動作。
if any features in your operating system aren’t incorporated into SWT, you can use the Java Native Interface to add them yourself.All it requires is a native Java method in the SWT package and a C function in the native graphics library that calls the operating system.
1.Display()--Allocates platform resources and creates a Display object
must be used in any SWT-based GUI.它產生一個Display類的實例并將其和GUI相聯系
2.getCurrent()--Returns the user-interface thread
must be used in any SWT-based GUI.它返回一個應用程序的主線程,用戶界面線程,通常和dispose()一起使用來結束Display的操作。
3.readAndDispatch()--Display object interprets events and passes them to receiver
enable the application to receive notifications from the operating system whenever the user takes an action associated with the GUI.
accesses the operating system’s event queue and determines whether any of the user’s actions are related to the GUI.
Using this method, the HelloSWT class knows whether the user has decided to dispose of the Shell. If so, the method returns TRUE, and the application ends. Otherwise, the Display object invokes its sleep() method, and the application continues waiting.
4.sleep()--Display object waits for events
enable the application to receive notifications from the operating system whenever the user takes an action associated with the GUI.