2.1 Programming in SWT

2.1 Programming in SWT
  • SWT应用程序的三段式结构:

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 类

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.

  • Display 类的方法

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.

  • Shell 类
  • The Shell class accesses the operating system through the OS class to an extent, but only to keep track of opening, activating, maximizing, minimizing, and closing the main window.

    The main function of the Shell class is to provide a common connection point for the containers, widgets, and events that need to be integrated into the GUI. Shell serves as the parent class to these components.

    attached to the Display的shell--top-level shells
    NOT directly attached to the Display instance的shell--secondary shell

    在你的GUI之内,你可以设定shell或其他小部件的风格参数值,若是多个值则可以用“|”相连.除了提到的属性,默认为“SHELL_TRIM”。

    SHELL_TRIM--有一个标题栏(SWT.TITLE)和用户可以最小化SWT.MIN)、最大化(SWT.MAX)、改变尺寸(SWT.RESIZE)和关闭(SWT.CLOSE)
    DIALOG_TRIM--有一个标题栏、一个活动区的边界(SWT.BORDER)和被关闭的能力

    你还可以确定shell的形态,以限定用户修改shell的modality,如A modal dialog box不能被移动或是改变尺寸,只可以使用给予的按钮关闭或是取消。


    NOT every platform can render these properties in GUI components.

    你可能感兴趣的:(2.1 Programming in SWT)