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可以动态调整工具栏的位置。
| 第1页: 菜单组件 | 第2页: 工具栏组件ToolBar和ToolItem |
| 第3页: 工具栏组件CoolBar和CoolItem | 第4页: 滚动组件Slider |
| 第5页: 刻度组件Scale | 第6页: 进度条组件ProgressBar |