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

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();
}
}
9 7 3 1 2 3 4 4 8 :

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

论坛美图

本周软件下载排行

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

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