技术开发 频道

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

工具栏组件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 工具栏组件


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

0
相关文章