技术开发 频道

ToolBar、SashForm 以及其他控件和对话框

  ToolBar 包含 ToolItem, ToolItem 可以有文本或图片作为按钮。一般来说,要么使用图片(AKA 图标)、要么使用文本,不能同时使用。如果仅使用图片,那么 ToolItem 需要有一个 ToolTip (帮助提供信息的短语或句子),这样当鼠标停留在 ToolItem 上时,会显示 ToolTip,解释图片的功能。

  ToolBar 必须定义成以下两种完全互异的样式之一:

  HORIZONTAL —— 水平方向的

  VERTICAL —— 垂直方向的

  ToolBar 支持以下样式:

  FLAT —— 以平面样式显示项目

  WRAP —— 项目换行

  RIGHT —— 项目右对齐(及左对齐)

  SHADOW_OUT —— 显示阴影

  ToolItem 必须定义成以下 5 种完全互异的样式之一:

  CHECK —— 可连续选择项目(复选)。

  DROP_DOWN —— 项目(通常)显示下拉菜单。

  PUSH —— 项目作为按钮,可直接引发动作(最常见的形式)。

  RADIO —— 此类项目只能选择一个。

  SEPARATOR —— 在项目组之间充当分隔符(通常是一个条),这个项目没有功能。

  以下两个清单分别显示了创建 ToolBar 和 ToolItem 的代码。

  清单 2. 创建 ToolBar 的方法

1 protected ToolBar createVToolBar(Composite parent, int style) {
2     return new ToolBar(parent, style | SWT.VERTICAL);
3 }
4
5 protected ToolBar createHToolBar(Composite parent, int style) {
6     return new ToolBar(parent, style | SWT.HORIZONTAL);
7 }
8

  清单 3. 创建 ToolItem 的方法

1 protected ToolItem createToolItem(ToolBar bar, int style, String text,
2                                   Image image, String tooltip,  
3                                   SelectionListener listener) {
4     if (image != null && (text == null && tooltip == null)) {
5         throw new IllegalArgumentException("image only items require a tool tip");
6     }
7     ToolItem ti = new ToolItem(bar, style);
8     if (image != null) {
9         ti.setImage(image);
10     }
11     else {
12         if (text != null) {
13             ti.setText(text);
14         }
15     }
16     if (tooltip != null) {
17         ti.setToolTipText(tooltip);
18     }
19     if (listener != null) {
20         ti.addSelectionListener(listener);
21     }
22     return ti;
23 }
24

  多数操作系统都提供了与系统状态关联的特殊形式的工具栏,称为 托盘 (Tray)。不能构造 Tray;必须使用 Display.getSystemTray() 访问 Tray。与 ToolBar 相似,托盘中包含 TrayItem。TrayItem 没有样式选项,通常以 PUSH ToolItem 的形式工作。

  CoolBar 类似 ToolBar,区别在于用户可以重新安排栏中的 CoolItem,可以在 CoolItem 之间拖曳分隔条。对于栏中的 CoolItem 如何换行以及栏中项目显示的顺序,CoolBar 还允许更多控制。CoolBar 只支持 FLAT (或 NONE)样式。CoolItem 只支持 DROP_DOWN 样式。与 ToolItem 不同,CoolItem 有相关的控件负责实际实现项目的 GUI。如果没有这个控件,CoolItem 就没有功能,通常也没有可视的实现。这个控件应当有一个 ToolTip。

  下面两个清单分别显示了创建 CoolBar 和 CoolItem 的代码。请注意只能创建水平 CoolBar。

  清单 4. 创建 CoolBar 的方法

1 protected CoolBar createCoolBar(Composite parent, int style) {
2     return new CoolBar(parent, style);
3 }
4

  清单 5. 创建 CoolItem 的方法

1 protected CoolItem createCoolItem(CoolBar bar, int style, String text,
2                                   Image image, SelectionListener listener) {
3     CoolItem ci = new CoolItem(bar, style);
4     if (text != null) {
5         ci.setText(text);
6     }
7     if (image != null) {
8         ci.setImage(image);
9     }
10     return ci;
11 }
12

  下面这个清单显示了如何用这个代码创建 CoolItem 和相关控件。在这个示例中,当选中 CoolItem 的控件时,会弹出一个菜单,如图 2 所示。请注意 CoolItem、按钮和菜单都用 CoolBar 作为双亲。还请注意 CoolItem 的文本没有显示,只显示了关联的控件。

  清单 6. CoolItem 的创建示例

1 CoolBar cb = createCoolBar(..., SWT.FLAT);
2   :  // other CoolItems
3 CoolItem ci = createCoolItem(cb, SWT.FLAT,  "Drop", null, null);
4 Button b = createButton(cb, SWT.PUSH, "Press Me 1", null, new SelectionAdapter() {
5     public void widgetSelected(SelectionEvent se) {
6         menu.setVisible(true);
7     }
8 });
9 ci.setControl(b);     // associate Button with CoolItem
10 Menu m = new Menu(cb);
11 createMenuItem(m, SWT.PUSH, "Item 1", null, '1', true, null);
12 createMenuItem(m, SWT.PUSH, "Item 2", null, '2', true, null);
13 createMenuItem(m, SWT.PUSH, "Item 2", null, '3', true, null);
14   :  // other CoolItems
15

  图 2. CoolItem 示例

0
相关文章