(1)创建ToolBar的时候,要有SWT.FLAT选项,否则小箭头显示不正确。 new ToolBar(shell,SWT.FLAT) (2)创建ToolItem时,使用SWT.DROP_DOWN选项,就会自动出小箭头了 new ToolItem(toolBar, SWT.DROP_DOWN); (3)点击小箭头的动作,要自己添加。一般是显示一个Menu,这个Menu也要自己实现。 举例下,xxx是一个ToolItem。
xxx.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { if (e.detail == SWT.ARROW) { //这里写点击小箭头时的处理,注意这个判断方法。 //以下是计算Menu的显示位置的。 Rectangle bounds = addMsg.getBounds(); Point point = toolBar.toDisplay(bounds.x, bounds.y + bounds.height); xxxMenu.setLocation(point); xxxMsgMenu.setVisible(true);
}else{ //这里写正常的点击大按钮时的处理。 } } });
以下文章是转的: ======================================================================= 工具栏通常有两种: toolbar、coolBar。两者的区分是CoolBar可以自由移动。
toolBar的实现通常有两种方式: 1、使用ToolBar和ToolItem; 2、使用ToolBarManager 、ActionContributionItem、Action组合;
先介绍第一种方式:使用ToolBar和ToolItem
package menu.test;
import org.eclipse.jface.action.Action; import org.eclipse.jface.action.MenuManager; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.widgets.Combo; import org.eclipse.swt.widgets.Display; 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.Shell; import org.eclipse.swt.widgets.Text; import org.eclipse.swt.widgets.ToolBar; import org.eclipse.swt.widgets.ToolItem;
public class ToolBarExample { Display display = new Display(); Shell shell = new Shell(display); ToolBar toolBar; Action forwardAction,homeAction; public ToolBarExample() { MenuManager menuManager = new MenuManager(); //1.添加工具栏 toolBar = new ToolBar(shell,SWT.FLAT|SWT.WRAP|SWT.RIGHT |SWT.BORDER);
//2.添加工具项-push ToolItem pushItem = new ToolItem(toolBar,SWT.PUSH); pushItem.setText("Push Item"); Image icon = new Image(shell.getDisplay(),"icons/forward.gif"); pushItem.setImage(icon); //3.添加工具项-check,radio,seperator ToolItem checkItem = new ToolItem(toolBar,SWT.CHECK); checkItem.setText("Check Item"); ToolItem radioItem1 = new ToolItem(toolBar, SWT.RADIO); radioItem1.setText("RADIO item 1"); ToolItem radioItem2 = new ToolItem(toolBar, SWT.RADIO); radioItem2.setText("RADIO item 2"); ToolItem separatorItem = new ToolItem(toolBar, SWT.SEPARATOR); //4.下拉DROP_DOWN、 final ToolItem dropDownItem = new ToolItem(toolBar, SWT.DROP_DOWN); dropDownItem.setText("DROP_DOWN item"); dropDownItem.setToolTipText("Click here to see a drop down menu ..."); final Menu menu = new Menu(shell, SWT.POP_UP); new MenuItem(menu, SWT.PUSH).setText("Menu item 1"); new MenuItem(menu, SWT.PUSH).setText("Menu item 2"); new MenuItem(menu, SWT.SEPARATOR); new MenuItem(menu, SWT.PUSH).setText("Menu item 3"); // 设置工具项的事件监听器 dropDownItem.addListener(SWT.Selection, new Listener() { public void handleEvent(Event event) { if (event.detail == SWT.ARROW) { Rectangle bounds = dropDownItem.getBounds(); Point point = toolBar.toDisplay(bounds.x, bounds.y + bounds.height); // 设置菜单的显示位置 menu.setLocation(point); menu.setVisible(true); } } }); // 5.事件处理 // 设置工具项的事件监听器 Listener selectionListener = new Listener() { public void handleEvent(Event event) { ToolItem item = (ToolItem) event.widget; System.out.println(item.getText() + " is selected"); if ((item.getStyle() & SWT.RADIO) != 0 || (item.getStyle() & SWT.CHECK) != 0) System.out.println("Selection status: " + item.getSelection()); } }; pushItem.addListener(SWT.Selection, selectionListener); checkItem.addListener(SWT.Selection, selectionListener); radioItem1.addListener(SWT.Selection, selectionListener); radioItem2.addListener(SWT.Selection, selectionListener); dropDownItem.addListener(SWT.Selection, selectionListener); // 6. 其他组件text 、checkbox ToolItem textItem = new ToolItem(toolBar,SWT.SEPARATOR); Text text = new Text(toolBar, SWT.BORDER | SWT.SINGLE); text.pack(); textItem.setWidth(text.getSize().x); textItem.setControl(text); ToolItem sep = new ToolItem(toolBar, SWT.SEPARATOR); Combo combo = new Combo(toolBar, SWT.READ_ONLY); for (int i = 0; i < 4; i++) { combo.add("Item " + i); } combo.pack(); sep.setWidth(combo.getSize().x); sep.setControl(combo); toolBar.pack(); shell.addListener(SWT.Resize, new Listener(){
public void handleEvent(Event arg0) { Rectangle clientArea = shell.getClientArea(); toolBar.setSize(toolBar.computeSize(clientArea.width, SWT.DEFAULT)); } }); shell.setSize(400, 300); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); } public static void main(String[] args) { new ToolBarExample(); }
}
对于第二种方法:
package menu; import org.eclipse.jface.action.Action; import org.eclipse.jface.action.ActionContributionItem; import org.eclipse.jface.action.MenuManager; import org.eclipse.jface.action.ToolBarManager; import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.widgets.Decorations; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.ToolBar;
public class ToolBarMangerExample { Display display = new Display(); Shell shell = new Shell(display); Action openAction,exitAction; ToolBar toolBar;
public ToolBarMangerExample() { MenuManager menuManager = new MenuManager(); toolBar = new ToolBar(shell, SWT.FLAT | SWT.RIGHT | SWT.BORDER); final ToolBarManager toolBarManager = new ToolBarManager(toolBar); initActions();// 初始化Action toolBarManager.add(openAction); ActionContributionItem item = new ActionContributionItem(exitAction); item.setMode(ActionContributionItem.MODE_FORCE_TEXT); toolBarManager.add(item); toolBarManager.update(true); toolBar.pack(); MenuManager fileMenuManager = new MenuManager("&File"); fileMenuManager.add(openAction); fileMenuManager.add(exitAction); menuManager.add(fileMenuManager); menuManager.updateAll(true); shell.setMenuBar(menuManager.createMenuBar((Decorations)shell)); shell.addListener(SWT.Resize, new Listener(){
public void handleEvent(Event arg0) { Rectangle clientArea = shell.getClientArea(); toolBar.setSize(toolBar.computeSize(clientArea.width, SWT.DEFAULT)); } }); shell.setSize(300,200); shell.open();
while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } }
display.dispose(); } private void initActions(){ openAction = new Action("&openAction", ImageDescriptor.createFromFile(null, "icons/Open.gif")) { public void run() { System.out.println("OPEN"); } }; openAction.setAccelerator(SWT.CTRL + 'O');
exitAction = new Action("&Exit", ImageDescriptor.createFromFile(null, "icons/Exit.gif")) { public void run() { System.out.println("Exit"); } }; exitAction.setAccelerator(SWT.CTRL + 'E'); } public static void main(String[] args) { new ToolBarMangerExample(); } }
|