8.3. 组合窗口组件Composite Widgets
组合窗口组件可以容纳其他的组合窗口组件。Composite类是组合窗口组件的父类。
图 10. Composite可以容纳其他的组合类
组合类可以容纳其他的组合类。这种容纳关系是用一个组合窗口组件类的构造函数来建造的。与Swing相比,SWT没有add() 方法;作为代替,你必须使用构造函数来建立包容关系结构。
正如图10中所看到的,Shell类也是一个组合类。也就是说,Shell对象,可以容纳其他的组合类。
组合类是可以卷起的,也就是说可以使用SWT.H_SCROLL和SWT.V_SCROLL常量给组合窗口组件加上滚动条。
8.3.1. Table窗口组件
Table窗口组件可以显示一批字符串项或者图片。与其他的组合窗口组件相比,不能给table窗口组件增加组合控件。Table窗口组件的子构件必须是TableItem类型的。
图 11. Table窗口组件
表7中的常量可以用于table窗口组件
表 7. SWT Table style bit常量 | |
SWT.MULTI | 使能够进行单一或者多项选择 |
SWT.FULL_SELECTION | 使能够进行行全选 |
SWT.CHECK | 在每一行的开始显示一个选择框 |
源代码 8 中的代码片断表明了含有两列的table组件的使用方法
源代码 8. Table窗口组件例子 |
final Table table = new Table(shell,SWT.SINGLE); TableColumn col1 = new TableColumn(table,SWT.LEFT); col1.setText("Coloumn 1"); col1.setWidth(80); TableColumn col2 = new TableColumn(table,SWT.LEFT); col2.setText("Coloumn 2"); col2.setWidth(80);
TableItem item1 = new TableItem(table,0); item1.setText(new String[]{"a","b"}); TableItem item2 = new TableItem(table,0); item2.setText(new String[]{"a","b"});
table.setHeaderVisible(true); table.setLinesVisible(true); |
8.3.2. Combo(组合框)窗口组件
Combo窗口组件允许用户从值列表中选择一个值或者随意输入一个新值。组合框(combo)类似于列表组件,却使用了有限的空间。
虽然组合框是组合性质的,但是对它增加子元素是没有意义的。它的元素必须是String类型。一元素可以使用Combo类中定义的add(String element)方法来添加到组合框中。
图 12. 不同样式的组合框
下边的SWT常量可以用于Combo窗口组件:
Table 8. SWT Combo style bit constants | |
SWT.DROP_DOWN | 下拉式组合框 |
SWT.READ_ONLY | 只读组合框 |
SWT.SIMPLE | 简单的组合框(非下拉式组合框)。如图11所示 |
下边的例子说明了Combo组件的使用:
Source 9. Combo example |
final Combo combo = new Combo(shell,SWT.DROP_DOWN); for (int i = 1; i < 11; i++) { combo.add(i+".) element "); } combo.setText("Text"); combo.addSelectionListener( new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { System.out.println("Selection:"+ combo.getText()); } } ); |
8.3.3. Tree窗口组件
Tree窗口组件描绘了树中项的可选择层次结构。虽然Tree类是组合的,但是不能对它增加组合类对象。Tree类的子项必须是ThreeItem类型。
图 13. 不同样式的Tree窗口组件
下表是Tree组件常量的列表。
表 9. SWT Tree style bit 常量 | |
SWT.SINGLE | 允许单选或者多选 |
SWT.CHECK | 在每个节点的开始显示一个选择框。 |
下边是一个简单的Tree组件例子。
源代码 10. Tree例子 |
final Tree tree = new Tree(shell,SWT.SINGLE); for (int i = 1; i < 11; i++) { final TreeItem item1 = new TreeItem(tree,SWT.NULL); item1.setText("node "+i); for (int j = 1; j < 6; j++) { final TreeItem item11 = new TreeItem(item1,SWT.NULL); item11.setText("node "+i+"."+j); } }
tree.addSelectionListener( new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { System.out.println("Selection:"+ tree.getSelection()[0]); } } ); |
8.3.4. TabFolder
TabFolder组件允许用户从一些页面中选择一页。虽然它是组合的,但是它不能增加其他的组合窗口组件。加到TabFolder的组件必须是TabItem类型。Tab的内容可以使用TabItem的setControl(Control control) 方法来设置。
图 14. TabFolder窗口组件
一个简单的TabFolder例子:
源代码 11. TabFolder例子 |
final TabFolder tabFolder = new TabFolder( shell, SWT.BORDER); for (int i=1; i<5; i++) { // create a TabItem TabItem item = new TabItem( tabFolder, SWT.NULL); item.setText( "TabItem " + i); // create a control Label label = new Label( tabFolder, SWT.BORDER); label.setText( "Page " + i); // add a control to the TabItem item.setControl( label ); } |
8.3.5. CoolBar窗口组件
CoolBar窗口组件提供了一个可以在动态摆放空间上增加项的区域。你可以增加一个或多个ToolBar组件到CoolBar上。一个CoolBar可一容纳一个或多个CoolItems。虽然是一个组合窗口组件,但是不能把其他组合类加到它上。CoolBar的子元素必须是CoolItem类型。
图 15. Coolbar窗口组件
CoolBar窗口组件使用的例子:
Source 12. CoolBar example |
CoolBar coolBar = new CoolBar(shell, SWT.BORDER); coolBar.setLayoutData( new FillLayout()); // create a tool bar which it // the control of the coolItem for (int k = 1; k <3; k++) { ToolBar toolBar = new ToolBar(coolBar, SWT.FLAT); for (int i = 1; i < 5; i++) { ToolItem item = new ToolItem(toolBar, SWT.NULL); item.setText("B"+k+"."+i); } // Add a coolItem to a coolBar CoolItem coolItem = new CoolItem(coolBar, SWT.NULL); // set the control of the coolItem coolItem.setControl(toolBar); // You have to specify the size Point size = toolBar.computeSize( SWT.DEFAULT, SWT.DEFAULT); Point coolSize = coolItem.computeSize (size.x, size.y); coolItem.setSize(coolSize); } |
8.4. 具有子项控件的概述
有些控件接受子组件作为子项。如,一个组合组件接受组合组件。有些组件只需要item(项)这样的组件如表10所列:
表 10. 有项的组件 | ||
窗口组件 | 项 | 描述 |
CoolBar | CoolItem | 项是可选择的,动态的摆放在CoolBar的区域上。 |
Menu | MenuItem | 项是菜单下的选择。 |
TabFolder | TabItem | 项是TabFolder中的Tab |
Table | TableItem | 项是表中的行。 |
ToolBar | ToolItem | 项是工具栏上的按钮。 |
Tree | TreeItem | 项是树上的节点。 |
结论
SWT是Eclipse用户接口的核心部分。Eclipse平台基于SWT库。要扩展你的SWT知识,你可以下载SWT网站上的SWT例子。