首页 > 新闻系统 > 编程天地 > 文章正文

Eclipse开发经典教程:展现组件

2008-02-15 11:08:25 来源:IT168 作者:阿甘 点击:
SWT中还有一些常用的组件,它们可以使组件更有表现力,且称之为展现组件,它们在SWT开发中也是不可缺少的,包括菜单组件、工具栏组件ToolBar和ToolItem、工具栏组件CoolBar和CoolItem、滚动组件Slider、刻度组件Scale和进度条组件ProgressBar等。

  
  

以上程序中添加了主菜单,并在主菜单中添加了两个子菜单项,子菜单项添加了相应的事件响应机制,程序运行效果如图1所示。


图1 Menu\MenuItem组件


菜单是可以级联的,在子菜单中还能够包含其它的菜单项。
  
工具栏组件ToolBar和ToolItem

ToolBar是SWT中的工具栏组件,ToolItem是工具栏中的工具项(一般表现为按钮或分隔符,也可以是其他组件),在程序中添加工具栏的步骤如下:

1. 创建ToolBar对象,并指定创建的样式,例如“toolBar = new ToolBar(shell, SWT.FLAT | SWT.WRAP | SWT.RIGHT);”。
2. 创建ToolItem对象,并指定创建样式,例如“ToolItem itemPush = new ToolItem (toolBar, SWT.PUSH);”。
3. 设置ToolItem的图标和相关属性,例如“itemPush.setImage(icon);”。
4. 添加ToolItem的事件监听器,例如“itemPush.addListener(SWT.Selection,selectionListener);”。

为了更好地掌握工具栏组件,下面通过一个实例演示如何创建工具栏组件,代码如例程2所示。

例程2 ToolBarExample.java
  
public class
ToolBarExample {
Display display
= new
Display();
Shell shell
= new
Shell(display);
ToolBar toolBar;
public
ToolBarExample() {
// 添加工具栏

toolBar = new ToolBar(shell, SWT.FLAT | SWT.WRAP | SWT.RIGHT);
// 添加工具项

ToolItem itemPush = new ToolItem(toolBar, SWT.PUSH);
itemPush.setText(
" PUSH item "
);
//
设置工具项的显示图标
//
Image icon = new Image(shell.getDisplay(), "icons/new.gif");
// itemPush.setImage(icon);

ToolItem itemCheck = new ToolItem(toolBar, SWT.CHECK);
itemCheck.setText(
" CHECK item "
);
ToolItem itemRadio1
= new
ToolItem(toolBar, SWT.RADIO);
itemRadio1.setText(
" RADIO item 1 "
);
ToolItem itemRadio2
= new
ToolItem(toolBar, SWT.RADIO);
itemRadio2.setText(
" RADIO item 2 "
);
ToolItem itemSeparator
= new
ToolItem(toolBar, SWT.SEPARATOR);
final ToolItem itemDropDown
= new
ToolItem(toolBar, SWT.DROP_DOWN);
itemDropDown.setText(
" DROP_DOWN item "
);
itemDropDown.setToolTipText(
" Click here to see a drop down menu ... "
);
final Menu menu
= new
Menu(shell, SWT.POP_UP);
new MenuItem(menu, SWT.PUSH).setText( " Menu item 1 "
);
new MenuItem(menu, SWT.PUSH).setText( " Menu item 2 "
);
new
MenuItem(menu, SWT.SEPARATOR);
new MenuItem(menu, SWT.PUSH).setText( " Menu item 3 "
);
// 设置工具项的事件监听器

itemDropDown.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event
) {
if ( event .detail ==
SWT.ARROW) {
Rectangle bounds
=
itemDropDown.getBounds();
Point point
=
toolBar.toDisplay(bounds.x, bounds.y
+
bounds.height);
// 设置菜单的显示位置

menu.setLocation(point);
menu.setVisible(
true
);
}
}
});
// 设置工具项的事件监听器

Listener selectionListener = new Listener() {
public void handleEvent(Event event
) {
ToolItem item
= (ToolItem) event
.widget;
System.
out .println(item.getText() + " is selected "
);
if ((item.getStyle() & SWT.RADIO) != 0

|| (item.getStyle() & SWT.CHECK) != 0 )
System.
out .println( " Selection status: "

+ item.getSelection());
}
};
itemPush.addListener(SWT.Selection, selectionListener);
itemCheck.addListener(SWT.Selection, selectionListener);
itemRadio1.addListener(SWT.Selection, selectionListener);
itemRadio2.addListener(SWT.Selection, selectionListener);
itemDropDown.addListener(SWT.Selection, selectionListener);
toolBar.pack();
shell.addListener(SWT.Resize,
new
Listener() {
public void handleEvent(Event event
) {
Rectangle clientArea
=
shell.getClientArea();
toolBar.setSize(toolBar.computeSize(clientArea.width,
SWT.DEFAULT));
}
});
shell.setSize(
400 , 100
);
shell.open();
while ( !
shell.isDisposed()) {
if ( !
display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static void
main(String[] args) {
new
ToolBarExample();
}
}
  

程序添加了工具栏,并在工具栏中添加了相应的工具项,工具项中添加了相应的事件响应机制,程序运行效果如图2所示。

 
图2 工具栏组件


本示例显示了工具栏和菜单栏的配合使用,菜单动态设定显示的位置。
  
工具栏组件CoolBar和CoolItem

CoolBar是另外一种形式的工具栏,它能够调整CoolItem的位置,每一个CoolItem可以设定相关的组件(也可以是另一个工具栏),创建CoolBar的步骤如下:

1. 创建CoolBar对象,并指定创建的样式,例如“CoolBar composite = new CoolBar (parent, SWT.NONE);”。
2. 创建CoolItem对象,并指定创建样式,例如“CoolItem item = new CoolItem(composite, SWT.NONE);”。
3. 设置CoolItem的Control对象,例如“item.setControl(tb);”。

CoolBar相当于一个面板容器,CoolItem是容器中的每一项,CoolItem能设置相应的组件作为此项的子容器(也可以是其他组件)。为了更好地掌握CoolBar组件,下面通过一个实例演示如何创建CoolBar组件,代码如例程3所示。

例程3 CoolBarExample.java
  
public class CoolBarExample extends ApplicationWindow {
public
CoolBarExample() {
super(
null
);
}
protected
Control createContents(Composite parent) {
getShell().setText(
" CoolBar Test "
);
String asCoolItemSection[]
= { " File " , " Formatting " , " Search "
};
// 添加CoolBar

CoolBar composite = new CoolBar(parent, SWT.NONE);
for ( int idxCoolItem = 0 ; idxCoolItem < 3 ; ++
idxCoolItem) {
CoolItem item
= new
CoolItem(composite, SWT.NONE);
// 添加子组件

ToolBar tb = new ToolBar(composite, SWT.FLAT);
for ( int idxItem = 0 ; idxItem < 3 ; ++
idxItem) {
ToolItem ti
= new
ToolItem(tb, SWT.NONE);
ti
.setText(asCoolItemSection[idxCoolItem]
+ " Item # "

+ idxItem);
}
Point p
=
tb.computeSize(SWT.DEFAULT, SWT.DEFAULT);
tb.setSize(p);
Point p2
=
item.computeSize(p.x, p.y);
// 设置为一个CoolItem的控制类

item.setControl(tb);
item.setSize(p2);
}
return
composite;
}
public static void
main(String[] args) {
CoolBarExample app
= new
CoolBarExample();
app.setBlockOnOpen(
true
);
app.open();
Display.getCurrent().dispose();
}
}
  

以上代码演示了如何创建CoolBar。CoolBar中每一个CoolItem可以根据用户的需要调整位置,程序运行效果如图3所示。


图3 CoolBar组件

 
CoolBar和ToolBar的展现样式不一样,CoolBar可以动态调整工具栏的位置。
  
滚动组件Slider

为了方便用户输入数据,SWT中提供了Slider组件,用户可通过Slider设置数据的增量值,用来控制其他组件,也可以作为滚动条控制其他组件中的数据显示。添加Slider组件的步骤如下:

1. 创建Slider对象,并指定创建的样式,例如“Slider slide = new Slider(shell, SWT.HORIZONTAL);”。
2. 设置Slider的最大值和最小值,例如“slide.setMaximum(100);”。
3. 设置Slider增量的增加或递减值,例如“slide.setIncrement(1);”。
4. 添加Slider的事件监听器,例如“slide.addSelectionListener(selectionListener);”。

为了更好地掌握Slider组件,下面通过一个实例演示如何创建Slider组件,代码如例程4所示。
例程4 SliderExample.java
public class SliderExample {
Display dispaly;
Shell shell;
SliderExample() {
dispaly
= new
Display();
shell
= new
Shell(dispaly);
shell.setSize(
300 , 250
);
shell.setText(
" A Slider Example "
);
// 添加Slider对象

final Slider slide = new Slider(shell, SWT.V_SCROLL);
// 设置Slider的位置和大小

slide.setBounds( 170 , 25 , 25 , 20 );
// 设置Slider的最小值

slide.setMinimum( 0 );
// 设置Slider的最大值

slide.setMaximum( 100 );
// 设置Slider单击左右箭头的增加或递减值

slide.setIncrement( 1 );
final Text t
= new
Text(shell, SWT.BORDER);
t.setBounds(
115 , 25 , 55 , 20
);
t.setText(
" 0 "
);
t.setFocus();
// 添加Slider的事件监听器

slide.addSelectionListener( new SelectionAdapter() {
public void
widgetSelected(SelectionEvent e) {
t.setText(
new
Integer(slide.getSelection()).toString());
}
});
shell.open();
while ( !
shell.isDisposed()) {
if ( !
dispaly.readAndDispatch())
dispaly.sleep();
}
dispaly.dispose();
}
public static void
main(String[] args) {
new
SliderExample();
}
}
  

以上代码添加了一个Text组件和一个Slider组件。Slider组件设置了增量值为1,另外Slider组件添加了选择事件,当选择了Slider组件后,Slider将为Text组件赋值。程序运行效果如图4所示。


图4 Slider组件


Slider组件要配合其它的组件使用,辅助其它的组件完成功能。
  
刻度组件Scale

Scale和Slider类似,在SWT中都表示一种尺度,但两者的表现形式不一样,Scale更像一个刻度,而Slider则是提供一个滚动条。添加Scale组件的步骤如下:

1. 创建Scale对象,并指定创建的样式,例如“Scale scale = new Scale(shell, SWT.VERTICAL);”。
2. 设置Scale的最大值和最小值,例如“scale.setMaximum(20);”。
3. 设置Scale增量的增加或递减值,例如“scale.setPageIncrement(5);”。
4. 添加Scale的事件监听器,例如“scale.addSelectionListener(selectionListener);”。

为了更好地掌握Scale组件,下面通过一个实例演示如何创建Scale组件,代码如例程5所示。

例程5 ScaleExample.java
public class ScaleExample {
Display display
= new
Display();
Shell shell
= new
Shell(display);
Scale scale;
Text value;
public
ScaleExample() {
shell.setLayout(
new GridLayout( 1 , true
));
Label label
= new
Label(shell, SWT.NULL);
label.setText(
" Volume: "
);
// 添加Scale组件

scale = new Scale(shell, SWT.VERTICAL);
scale.setBounds(
0 , 0 , 40 , 200
);
// 设置Scale的最大值

scale.setMaximum( 20 );
// 设置Scale的最小值

scale.setMinimum( 0 );
// 设置Scale的增量值

scale.setPageIncrement( 5 );
// 添加Scale的事件监听器

scale.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event
) {
int perspectiveValue = scale.getMaximum() - scale.getSelection() +

scale.getMinimum();
value.setText(
" Vol: " + perspectiveValue);
}
});
value
= new Text(shell, SWT.BORDER |
SWT.SINGLE);
value.setEditable(
false
);
scale.setLayoutData(
new
GridData(GridData.HORIZONTAL_ALIGN_CENTER));
value.setLayoutData(
new
GridData(GridData.HORIZONTAL_ALIGN_CENTER));
shell.pack();
shell.open();
while ( !
shell.isDisposed()) {
if ( !
display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
private void
init() {
}
public static void
main(String[] args) {
new
ScaleExample();
}
}
  

上例中,通过事件监听器监听当前选择的刻度,并用Text组件显示出来,程序运行效果如图5所示。


图5 Scale组件


Scale组件能够精确的显示刻度,用户可以设制好刻度的范围,这是非常有用的。
  
进度条组件ProgressBar

ProgressBar是SWT中的进度条组件。进度条提供了比较长时间操作的进度信息。添加ProgressBar组件的步骤如下:
1. 创建ProgressBar对象,并指定创建的样式,例如“ProgressBar pb1 = new ProgressBar (shell, SWT.HORIZONTAL | SWT.SMOOTH);”。
2. 设置ProgressBar的最大值和最小值,例如“pb1.setMaximum(30);”。
3. 在长时间的任务中设置当前进度条的进度,例如“progressBar.setSelection (progressBar.getSelection() + 1);”。

进度条能反映当前的工作进度,为了配合处理长时间的任务,进度条经常配合线程使用,以免产生阻塞影响界面的操作。为了更好地掌握ProgressBar组件,下面通过一个实例演示如何创建ProgressBar组件,代码如例程6所示。

例程6 ProgressBarExample.java
public class ProgressBarExample {
public static void
main(String[] args) {
Display display
= new
Display();
Shell shell
= new
Shell(display);
shell.setLayout(
new
GridLayout());
// 添加平滑的进度条

ProgressBar pb1 = new ProgressBar(shell, SWT.HORIZONTAL | SWT.SMOOTH);
pb1.setLayoutData(
new
GridData(GridData.FILL_HORIZONTAL));
// 设置进度条的最小值

pb1.setMinimum( 0 );
// 设置进度条的最大值

pb1.setMaximum( 30 );
// 添加自动递增的进度条

ProgressBar pb2 = new ProgressBar(shell, SWT.HORIZONTAL |
SWT.INDETERMINATE);
pb2.setLayoutData(
new GridData(GridData.FILL_HORIZONTAL));
// 添加线程,在线程中处理长时间的任务,并最终反映在平滑进度条上

new LongRunningOperation(display, pb1).start();
shell.open();
while ( !
shell.isDisposed()) {
if ( !
display.readAndDispatch()) {
display.sleep();
}
}
}
}
class
LongRunningOperation extends Thread {
private
Display display;
private
ProgressBar progressBar;
public
LongRunningOperation(Display display, ProgressBar progressBar) {
this .display =
display;
this .progressBar =
progressBar;
}
public void
run() {
// 模仿长时间的任务

for ( int i = 0 ; i < 30 ; i ++ ) {
try
{
Thread.sleep(
1000
);
}
catch
(InterruptedException e) {
}
display.asyncExec(
new
Runnable() {
public void
run() {
if (progressBar.isDisposed()) return
;
// 进度条递增

progressBar.setSelection(progressBar.getSelection() + 1 );
}
});
}
}
}
  

以上代码添加了两个进度条,一个进度条为自动显示增加进度的信息(SWT.INDETERMINAT样式),另外一个进度条通过线程处理长时间的任务,并设定进度条的信息。程序运行效果如图6所示。


图6 ProgressBar组件


进度条有不同的样式,在程序中,开发人员可以控制进度条的进度,执行某些长时间的操作。

9 7 3 1 2 3 4 4 8 :

精彩推荐
焦点大图推荐
本类热门文章

论坛美图

本周软件下载排行

广告联系 | 版权说明 | 意见建议 | 加入收藏 | 军网站群 [ 军软件园 - 军软件商城 - 军软件园论坛 ]

电信与信息服务业务经营许可证:京ICP证050203