[工作][JFACE,SWT] menu, toolBar
2015-06-09 10:11
246 查看
展现组件;
SWT中还有一些常用的组件,它们可以使组件更有表现力,且称之为展现组件,它们在SWT开发中也是不可缺少的,包括菜单组件、工具栏组件ToolBar和ToolItem、工具栏组件CoolBar和CoolItem、滚动组件Slider、刻度组件Scale和进度条组件ProgressBar等。
菜单组件
在程序中,菜单是提供信息比较好的方式,SWT中通过Menu和MenuItem实现菜单和菜单项,在程序中添加菜单的步骤如下。
(1)创建Menu对象,并指定创建的样式,例如“menuBar = new Menu(shell, SWT.BAR);”。
(2)创建MenuItem对象,并指定创建样式,例如“fileEnglishItem = new MenuItem (fileMenu, SWT.RADIO);”。
(3)设置Menu和MenuItem的关联(Menu中还可以有子Menu),例如“fileMenuHeader. setMenu(fileMenu);”。
(4)添加MenuItem的事件监听器,例如“fileEnglishItem.addSelectionListener(new RadioItemListener());”。
为了更好地掌握菜单,下面通过一个实例演示如何创建菜单,代码如例程1所示。
例程1 MenuExample.java
以上程序中添加了主菜单,并在主菜单中添加了两个子菜单项,子菜单项添加了相应的事件响应机制,程序运行效果如图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
程序添加了工具栏,并在工具栏中添加了相应的工具项,工具项中添加了相应的事件响应机制,程序运行效果如图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所示。
SWT中还有一些常用的组件,它们可以使组件更有表现力,且称之为展现组件,它们在SWT开发中也是不可缺少的,包括菜单组件、工具栏组件ToolBar和ToolItem、工具栏组件CoolBar和CoolItem、滚动组件Slider、刻度组件Scale和进度条组件ProgressBar等。
菜单组件
在程序中,菜单是提供信息比较好的方式,SWT中通过Menu和MenuItem实现菜单和菜单项,在程序中添加菜单的步骤如下。
(1)创建Menu对象,并指定创建的样式,例如“menuBar = new Menu(shell, SWT.BAR);”。
(2)创建MenuItem对象,并指定创建样式,例如“fileEnglishItem = new MenuItem (fileMenu, SWT.RADIO);”。
(3)设置Menu和MenuItem的关联(Menu中还可以有子Menu),例如“fileMenuHeader. setMenu(fileMenu);”。
(4)添加MenuItem的事件监听器,例如“fileEnglishItem.addSelectionListener(new RadioItemListener());”。
为了更好地掌握菜单,下面通过一个实例演示如何创建菜单,代码如例程1所示。
例程1 MenuExample.java
public class MenuExample { Display display; Shell shell; Menu menuBar, fileMenu, editMenu; MenuItem fileMenuHeader, editMenuHeader; MenuItem fileExitItem, fileSaveItem, fileEnglishItem, fileGermanItem, editCopyItem; Text text; public MenuExample() { display = new Display(); shell = new Shell(display); shell.setText("Menu Example"); shell.setSize(300, 200); text = new Text(shell, SWT.BORDER); text.setBounds(80, 50, 150, 25); //添加主菜单项 menuBar = new Menu(shell, SWT.BAR); //添加一级子菜单 fileMenuHeader = new MenuItem(menuBar, SWT.CASCADE); fileMenuHeader.setText("&File"); //添加一级子菜单的菜单项 fileMenu = new Menu(shell, SWT.DROP_DOWN); fileMenuHeader.setMenu(fileMenu); fileSaveItem = new MenuItem(fileMenu, SWT.PUSH); fileSaveItem.setText("&Save"); fileEnglishItem = new MenuItem(fileMenu, SWT.RADIO); fileEnglishItem.setText("English"); fileGermanItem = new MenuItem(fileMenu, SWT.RADIO); fileGermanItem.setText("German"); fileExitItem = new MenuItem(fileMenu, SWT.PUSH); fileExitItem.setText("E&xit"); editMenuHeader = new MenuItem(menuBar, SWT.CASCADE); editMenuHeader.setText("&Edit"); editMenu = new Menu(shell, SWT.DROP_DOWN); editMenuHeader.setMenu(editMenu); editCopyItem = new MenuItem(editMenu, SWT.PUSH); editCopyItem.setText("&Copy"); //添加菜单项的事件监听器 fileExitItem.addSelectionListener(new MenuItemListener()); fileSaveItem.addSelectionListener(new MenuItemListener()); editCopyItem.addSelectionListener(new MenuItemListener()); fileEnglishItem.addSelectionListener(new RadioItemListener()); fileGermanItem.addSelectionListener(new RadioItemListener()); shell.setMenuBar(menuBar); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } class MenuItemListener extends SelectionAdapter { public void widgetSelected(SelectionEvent event) { text.setText("You selected " + ((MenuItem) event.widget).getText()); if (((MenuItem) event.widget).getText().equals("E&xit")) { shell.close(); } } } class RadioItemListener extends SelectionAdapter { public void widgetSelected(SelectionEvent event) { MenuItem item = (MenuItem) event.widget; text.setText(item.getText() + " is on."); } } public static void main(String[] args) { MenuExample menuExample = new MenuExample(); } }
以上程序中添加了主菜单,并在主菜单中添加了两个子菜单项,子菜单项添加了相应的事件响应机制,程序运行效果如图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; cdb7 } public static void main(String[] args) { CoolBarExample app = new CoolBarExample(); app.setBlockOnOpen(true); app.open(); Display.getCurrent().dispose(); } }
相关文章推荐
- iOS中nil、Nil、NULL、NSNull详解
- Android图片的固定大小展示
- 简介JavaScript中的push()方法的使用
- 字节序的那些事情
- 巧说table的几种构造方式
- 求最大重叠区间大小
- 跨平台工程移植:opencv_pnp + artoolkitplus :ubuntu qtcreator--> windows qtcreator/ VS
- 软件项目管理面面观之“玩的就是心跳”
- jquery获取和设置radio,select,checkbox的值
- mybatis特性--- 级联查询
- 九度OJ-题目1524:复杂链表的复制
- 如何配置git ssh连接,如何在GitHub上添加协作开发者,如何配置gitignore和如何在GitHub上删除资源库.
- 文件操作中SaveFileDialog的用法
- bitcoin多重签名
- Android短信发送和接收流程源码分析
- SharePoint 2013 开发——SharePoint Designer 2013工作流
- 增加虚拟主机
- ssh 配置讲解大全 [待整理]
- iostat 等运维命令用法
- iptables详解