ToolBar与ToolItem
ToolBar是用来放置工具按钮ToolItem的容器。ToolItem能同时显示设置图像和文字。ToolItem有多种风格样式:
- ToolItem:普通风格的工具条按钮;
- Check ToolItem:复选框样式的工具条按钮;
- Radio ToolItem:单选框样式的工具条按钮,同样具有排他性。
- DropDown ToolItem:下拉样式的工具条按钮,配合Menu控件使用。
- Separator ToolItem:分隔条效果的工具条按钮。
创建工具条和按钮
源码
ToolBarComposite
继承Composite类,包含各样式的工具条按钮。
package mailrcp.swt;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
public class ToolBarComposite extends Composite {
/**
* 构造函数
*
* @param parent 父面板
* @param style 样式
*/
public ToolBarComposite(Composite parent, int style) {
super(parent, style);
setLayout(new GridLayout(1, false));
final ToolBar toolBar = new ToolBar(this, SWT.FLAT | SWT.WRAP | SWT.RIGHT);
toolBar.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
// ### 普通风格的工具条按钮 ###
ToolItem toolItem1 = new ToolItem(toolBar, SWT.NONE);
toolItem1.setText("ToolItem1");
ToolItem toolItem2 = new ToolItem(toolBar, SWT.NONE);
toolItem2.setText("ToolItem2");
new ToolItem(toolBar, SWT.SEPARATOR);
// ### 复选框样式的工具条按钮 ###
ToolItem checkToolItem1 = new ToolItem(toolBar, SWT.CHECK);
checkToolItem1.setText("Check Item1");
ToolItem checkToolItem2 = new ToolItem(toolBar, SWT.CHECK);
checkToolItem2.setText("Check Item2");
new ToolItem(toolBar, SWT.SEPARATOR);
// ### 单选框样式的工具条按钮 ###
ToolItem radioToolItem1 = new ToolItem