第五节 剖析Shell对象
Shell对象代表了一个窗口——要么是个顶级窗口,要么是个对话框。它容纳(contain)了各种各样的控件以构建应用程序:按钮、文本框、表格等等。它有六个构造函数,其中两个已不推荐使用,而且将来的发行版可能不再支持它们。构造过程沿用了SWT传递一个容器(parent)和一个样式(或者多个样式的位与or),有些构造函数允许单个或两个参数使用默认值。表3-3列出了构造函数。
表3-3 Shell的构造函数
说明 | |
public Shell() | Empty constructor, which is equivalent to calling Shell((Display) null). Currently, passing null for the Display causes the Shell to be created on the active display, or, if no display is active, on a "default" display. This constructor is discouraged, and might be removed from a future SWT release. |
public Shell(int style) | This constructor, too, isn't recommended for use, as it calls Shell((Display) null, style), so also might be removed from SWT. |
public Shell(Display display) | Constructs a shell using display as the display, null for the parent, and SHELL_TRIM for the style, except on Windows CE, where it uses NONE (see Table 3-4). |
public Shell(Display display, int style) | Constructs a shell using display as the display, null for the parent, and style for the style. See Table 3-4 for appropriate Shell styles. |
public Shell(Shell parent) | Constructs a shell using the parent's Display as the display, parent for the parent, and DIALOG_TRIM for the style, except on Windows CE, where it uses NONE (see Table 3-4). |
public Shell(Shell parent, int style) | Constructs a shell using the parent's Display as the display, parent for the parent, and style for the style. See Table 3-4 for appropriate Shell styles. |
表3-4: Shell的样式
说明 | |
BORDER | Adds a border. |
CLOSE | Adds a close button. |
MIN | Adds a minimize button. |
MAX | Adds a maximize button. |
NO_TRIM | Creates a Shell that has no border and can't be moved, closed, resized, minimized, or maximized. Not very useful, except perhaps for splash screens. |
RESIZE | Adds a resizable border. |
TITLE | Adds a title bar. |
DIALOG_TRIM | Convenience style, equivalent to TITLE | CLOSE | BORDER. |
SHELL_TRIM | Convenience style, equivalent to CLOSE | TITLE | MIN | MAX | RESIZE. |
APPLICATION_MODAL | Creates a Shell that's modal to the application. Note that you should specify only one of APPLICATION_MODAL, PRIMARY_MODAL, SYSTEM_MODAL, or MODELESS; you can specify more, but only one is applied. The order of preference is SYSTEM_MODAL, APPLICATION_MODAL, PRIMARY_MODAL, then MODELESS. |
PRIMARY_MODAL | Creates a primary modal Shell. |
SYSTEM_MODAL | Creates a Shell that's modal system-wide. |
MODELESS | Creates a modeless Shell. |
所有的构造函数都调用包内可见的构造函数,用它设置display,样式、容器(parent),然后创建窗体。如果Shell有容器(parent),它就是一个对话框;否则,它就是顶级窗口。表3-4列出了Shell对象可能的样式;注意,我们将在下一节看到,所有的样式常量,都是SWT类的静态(static)成员。同时,也要注意设置的样式仅仅被作为提示(hint)被对待,当程序允许的平台上不支持这种样式时,它会被忽略。
大多数时候,创建Shell时不需要指出样式,因为默认的设置通常就是客户想要的。自己试验下各种样式,这样就可以了解它们。
Shell从它的继承树中继承了大量的方法,并且添加了一些自己的方法(Shell特有的全部方法列表请参见表3-5)。但是经常用到的两个方法是open()和close(),其中open()打开(显示)Shell,close()关闭Shell。注意,默认的操作平台已经实现了关闭Shell的方法(例如,单击标题栏的关闭按钮),所以程序员可能从来都不用调用close()。
表 3-5: Shell 的方法
说明 | |
void addShellListener (ShellListener listener) | Adds a listener that's notified when operations are performed on the Shell. |
void close() | Closes the Shell. |
void dispose() | Disposes the Shell, and recursively disposes all its children. |
void forceActive() | Moves the Shell to the top of the z-order on its Display and forces the window manager to make it active. |
Rectangle getBounds() | Returns the Shell's size and location relative to its parent (or its Display in the case of a top-level Shell). |
Display getDisplay() | Returns the Display this Shell was created on. |
boolean getEnabled() | Returns true if this Shell is enabled, and false if not. |
int getImeInputMode() | Returns this Shell's input-method editor mode, which is the result of bitwise ORing one or more of SWT.NONE, SWT.ROMAN, SWT.DBCS, SWT.PHONETIC, SWT.NATIVE, and SWT.ALPHA. |
Point getLocation() | Returns the location of this Shell relative to its parent (or its Display in the case of a top-level Shell). |
Region getRegion() | Returns this Shell's region if it's nonrectangular. Otherwise, returns null. |
Shell getShell() | Returns a reference to itself. |
Shell[] getShells() | Returns all the Shells that are descendants of this Shell. |
Point getSize() | Returns this Shell's size. |
boolean isEnabled() | See getEnabled(). |
void open() | Opens (displays) this Shell. |
void removeShellListener (ShellListener listener) | Removes the specified listener from the notification list. |
void setActive() | Moves the Shell to the top of the z-order on its Display and asks the window manager to make it active. |
void setEnabled(boolean enabled) | Passing true enables this Shell; passing false disables it. |
void setImeInputMode (int mode) | Sets this Shell's input-method editor mode, which should be the result of bitwise ORing one or more of SWT.NONE, SWT.ROMAN, SWT.DBCS, SWT.PHONETIC, SWT.NATIVE, and SWT.ALPHA. |
void setRegion(Region region) | Sets the region for this Shell. Use for nonrectangular windows. |
void setVisible(boolean visible) | Passing true sets this Shell visible; passing false sets it invisible. |