频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

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

来源: 作者:佚名 出处:巧巧读书 2008-02-19 进入讨论组
上一页 1 2 3 4 5 6 下一页 

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

Eclipse开发经典教程:展现组件(图二) 
图2 工具栏组件


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

URl收藏 http://www.qqread.com/java/2008/02/r397064.html 更多文章 更多内容请看Eclipse应用技术ASP.NET教程Wlan组网----家庭专题专题,或进入讨论组讨论。
上一页 1 2 3 4 5 6 下一页 
收藏此文】【 】【打印】【关闭
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
讨论组问题推荐
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章