作者qrtt1 (隐者)
标题[doc] 基本元件的使用
时间Sat Apr 22 07:27:31 2006
Title: 基本元件的使用
Requirement: 了解SWT程式的基本架构
本文说所的「基本元件」仅为作者个人的讲法,对於尚未使用SWT撰写GUI
的人来说。能先在视窗中布置一些元件有助於增长信心的。而这些基本元
件共用的特性为:仅以最简单的操作形式就能使用。本文说要介绍的基本
元件有:Label、Text、Button、List、Combo。此外,还有将元件群组或
组装功能的元件:Composite、Group。
下列程式码为SWT 的骨架,以注解的方式标明元件合理的配置位置。每一
个SWT 程式,至少需要一个特殊的Display 类别,负责控制SWT 本身与作
业系统间的通讯,再以一个Shell 类别,持有真正的视窗。而元件则要布
置在Shell 产生的视窗之上。因此,元件合理的产生位置。应该在Shell
实例产生之後至Shell 实例释放之前。
==============================================================================
public class SWT {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
/*
元件配置区
*/
shell.setSize(400, 300);
shell.setText("SWT");
shell.open();
while (!shell.isDisposed())
if (!display.readAndDispatch()) {
display.sleep();
}
display.dispose();
}
}
==============================================================================
在正式使用元件之前,先浏览一下物类的继承关系:
以Button为例[1],
java.lang.Object
org.eclipse.swt.widgets.Widget
org.eclipse.swt.widgets.Control
org.eclipse.swt.widgets.Button
在Button与其抽象类别之间,尚有一个Control类别负责处理使用者与视
视窗之间互动的行为。像焦点(focus) 控制,注册事件接收器等等。都依
赖Control类别来控制。而更上一层为所有元件的抽象类别Widget ,也是
规范元件产的主要介面。Widget类别的建构式只有一个,需指定一个父元
件(parent)以及此Widget类别的风格(style):
Widget(Widget parent, int style)
本文所介绍的基本元件都继承自Widget,所以配置元件都使用此建构式。
而建构式中parent参数应为何者呢?再次回顾SWT 程式基本架构。需先产
生一个沟通SWT 程式与系统底层的Display,再以此Display为一特殊的不
可视元件作为Shell 的父元件将真实的视窗呈现於画面。而其他的元件可
以为Shell 的子元件。因此,目前所知,我们及将产生的元件,父元件可
指定为Shell。至於style则使用SWT 类别定义的常数来决定。可供指定的
style也依元件有所不同,可查阅API後再决定设定的内容。以Button类别
为例:
以下为Button类别的手册部分内容,每一个元件皆有三个部分的主要描述
。Styles 说明该类别能使用的style;Events列出可供注册的事件;Note
则附加说明Styles 使用的限制。由Note可看出,style大致分为二组,一
为指定按钮型态的style:ARROW、CHECK、PUSH、RADIO与TOGGLE。一为指
定按钮内文字对齐方式的:LEFT、RIGHT与CENTER 。此外,还附加说明,
当style为ARROW需指定ARROW的方向:UP、DOWN、LEFT与RIGHT。有这样的
了解就能依类别说明自行查阅。
<%
Styles:
ARROW, CHECK, PUSH, RADIO, TOGGLE, FLAT
UP, DOWN, LEFT, RIGHT, CENTER
Events:
Selection
Note: Only one of the styles ARROW, CHECK, PUSH, RADIO, and TOGGLE
may be specified.
Note: Only one of the styles LEFT, RIGHT, and CENTER may be specified.
Note: Only one of the styles UP, DOWN, LEFT, and RIGHT may be specified
when the ARROW style is specified.
IMPORTANT: This class is intended to be subclassed only within the SWT
implementation.
%>
看懂了手册的描述後,就能试着产生Button 类别。如前文所述,style常
数定义於SWT内,并且皆为static 。所以,你可以用下列程式码产生一个
Button。
<%
Button b = new Button(shell, SWT.PUSH);
%>
而若要同时指定,文字靠右对齐,可改写为:
<%
Button b = new Button(shell, SWT.PUSH | SWT. RIGHT);
%>
其他的元件:Label、Text、List、Combo。用法则与Button相似。最後,
提供一个完整可执行的范例:
=========================================================================
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class ButtonEx {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
Button button = new Button(shell, SWT.PUSH);
button.setText("I am a button");
/*
使用pack method将button调整至少需要的大小。
在不使用版面管理员的情况下,
若无设定大小,则元件会呈现於shell中的一个小点。
*/
button.pack();
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
=========================================================================
[1] 注意:非所有本文列举的基本元件都是如此的继承关系。
--
我所追求的纯粹只是任性,完全的任性。 -- 挪威的森林
--
※ Origin: SayYA 资讯站 <bbs.sayya.org>
◆ From: pc210-59-94-118.nutn.edu.tw
◆ Modify: 05/08/17 9:04:01 <pc210-59-94-118.nutn.edu.tw>
※ X-Info: qrtt1 -> [email protected]
※ X-Sign: 124IQHH0chkS2kH6BT2Y (06/04/22 7:26:41 )
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 210.59.94.148
※ 编辑: qrtt1 来自: 210.59.94.148 (04/22 07:27)