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.