技术开发 频道

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

工具栏组件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; } public static void main(String[] args) { CoolBarExample app = new CoolBarExample(); app.setBlockOnOpen(true); app.open(); Display.getCurrent().dispose(); } }

以上代码演示了如何创建CoolBar。CoolBar中每一个CoolItem可以根据用户的需要调整位置,程序运行效果如图3所示。


图3 CoolBar组件

 
CoolBar和ToolBar的展现样式不一样,CoolBar可以动态调整工具栏的位置。

0
相关文章