SWT-Menu篇
编程技术  /  houtizong 发布于 3年前   117
今天用到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_LEFTmenuBar = 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");}// 事件处理{// pushItemfilePushItem.addListener(SWT.Selection, new Listener() {public void handleEvent(Event event) {text.setText(((MenuItem) event.widget).getText()+ "Action performed!");}});// radioradioItem1.addListener(SWT.Selection, new Listener() {public void handleEvent(Event e) {text.setText("Radio item toggled to:"+ radioItem1.getSelection());}});// checkcheckItem1.addListener(SWT.Selection, new Listener() {public void handleEvent(Event e) {text.setText("Check item toggled to:"+ checkItem1.getSelection());}});// exitexitItem.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。
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成
网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];
文章归档
文章标签
友情链接