今天用到Menu,本以为小菜一碟,都是老掉牙的东东了还不简单。逮着键盘狂敲,直到运行的时候,却发现和想象有点出入,bugging。。。
虽然很快就解决了问题,但是自己咬到自己舌头的滋味不好受,索性简单复习一下功课。前人言,好记性不如烂笔头。
package menu; import org.eclipse.swt.SWT; import org.eclipse.swt.events.HelpEvent; import org.eclipse.swt.events.HelpListener; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; 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; public class MenuExample1 { Display display; Shell shell; Menu menuBar, fileMenu, secondMenu_Radio, secondMenu_Check, helpMenu; MenuItem fileMenuHeader, helpMenuHeader; MenuItem filePushItem, fileSeperatorItem, exitItem, helpItem, secondMenu_RadioItem, radioItem1, radioItem2, secondMenu_CheckItem, checkItem1, checkItem2; Text text; public MenuExample1() { display = new Display(); shell = new Shell(display); shell.setText("Menu Example"); shell.setSize(300, 200); text = new Text(shell, SWT.BORDER); text.setBounds(80, 50, 150, 50); // 在shell的顶部创建菜单区域,菜单栏的样式必须为SWT.BAR。 // 可用的样式包括:BAR, DROP_DOWN, POP_UP, NO_RADIO_GROUP,LEFT_TO_RIGHT, // RIGHT_TO_LEFT menuBar = new Menu(shell, SWT.BAR); { // 创建File菜单 fileMenuHeader = new MenuItem(menuBar, SWT.CASCADE);// 确保样式是SWT.CASCADE,即级联菜单,这样才会弹出下拉菜单。 fileMenuHeader.setText("&File"); // 追加菜单 fileMenu = new Menu(shell, SWT.DROP_DOWN); fileMenuHeader.setMenu(fileMenu); filePushItem = new MenuItem(fileMenu, SWT.PUSH); filePushItem.setText("&PushMenuItem"); // 分割栏 fileSeperatorItem = new MenuItem(fileMenu, SWT.SEPARATOR); // 子菜单 secondMenu_RadioItem = new MenuItem(fileMenu, SWT.CASCADE); secondMenu_RadioItem.setText("RadioMenu"); secondMenu_Radio = new Menu(shell, SWT.DROP_DOWN); secondMenu_RadioItem.setMenu(secondMenu_Radio); radioItem1 = new MenuItem(secondMenu_Radio, SWT.RADIO); radioItem1.setText("Radio1"); radioItem2 = new MenuItem(secondMenu_Radio, SWT.RADIO); radioItem2.setText("Radio3"); secondMenu_CheckItem = new MenuItem(fileMenu, SWT.CASCADE); secondMenu_CheckItem.setText("Check"); secondMenu_Check = new Menu(shell, SWT.DROP_DOWN); secondMenu_CheckItem.setMenu(secondMenu_Check); checkItem1 = new MenuItem(secondMenu_Check, SWT.CHECK); checkItem1.setText("Check1"); checkItem2 = new MenuItem(secondMenu_Check, SWT.CHECK); checkItem2.setText("Check2"); // checkItem1.setImage(image) } { // 然后通过setAccelerator来设置快捷键,为CTRL+x, // 此时用户可以不需要打开菜单就可以执行该操作 helpMenuHeader = new MenuItem(menuBar, SWT.CASCADE); helpMenuHeader.setText("&Help"); helpMenu = new Menu(shell, SWT.DROP_DOWN); helpMenuHeader.setMenu(helpMenu); exitItem = new MenuItem(helpMenu, SWT.PUSH); exitItem.setText("E&xit\tCtrl+X"); exitItem.setAccelerator(SWT.CTRL + 'X'); helpItem = new MenuItem(helpMenu, SWT.PUSH); helpItem.setText("Help\tF1"); } // 事件处理 { // pushItem filePushItem.addListener(SWT.Selection, new Listener() { public void handleEvent(Event event) { text.setText(((MenuItem) event.widget).getText() + "Action performed!"); } }); // radio radioItem1.addListener(SWT.Selection, new Listener() { public void handleEvent(Event e) { text.setText("Radio item toggled to:" + radioItem1.getSelection()); } }); // check checkItem1.addListener(SWT.Selection, new Listener() { public void handleEvent(Event e) { text.setText("Check item toggled to:" + checkItem1.getSelection()); } }); // exit exitItem.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent arg0) { shell.close(); } }); // 帮助监听器HelpListener,通过按下F1/Help键,HelpListener被触发 helpItem.addHelpListener(new HelpListener() { public void helpRequested(HelpEvent e) { text.setText("Help requested on SubAction"); } }); } shell.setMenuBar(menuBar); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } public static void main(String[] args) { MenuExample1 menuExample = new MenuExample1(); } }
如果想变成右键快捷菜单,那就更简单了:将上面的代码稍加处理就可以了。如下:
menuBar = new Menu(shell, SWT.BAR);》》》》》》》》》menuBar = new Menu(shell, SWT.POP_UP);
shell.setMenuBar(menuBar);》》》》》》》》》shell.setMenu(menuBar);
ok,over。