• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

swt tray demo:右键菜单,单击隐藏,所有事件的例子

发布: 2007-7-01 18:47 | 作者: admin | 来源: | 查看: 12次 | 进入软件测试论坛讨论

领测软件测试网
swt的system tray,只能处理三种事件:左键单击,左键双击和右键双击,右键单击这是我看swt 3.0.1 win32的源码所知此demo有以下功能:1.左键单击时 ,隐藏,显示主窗口2.右键单击,弹出菜单注意:1.左键双击和右键双击是一种事件,无法区分2.左键双击将产生以下事件:左键单击,左键双击,左键单击3.关于弹出菜单,menu必须有一个parent,如果程序里没有任何shell,可以建一个不可见的shell专门作为menu的parent.

import org.eclipse.swt.*;import org.eclipse.swt.widgets.*;import org.eclipse.swt.events.*;import org.eclipse.swt.layout.FillLayout;import org.eclipse.swt.graphics.Image;class HelloTray implements SelectionListener{Display display;Shell shell;Button hello;Tray tray;TrayItem ti;Menu menu;MenuItem mi1;MenuItem mi2;MenuItem quit;public HelloTray(){display = Display.getDefault();shell = new Shell();FillLayout fillLayout = new FillLayout();fillLayout.type = SWT.VERTICAL;shell.setLayout(fillLayout);hello = new Button(shell, SWT.NONE);hello.setText("Hello World!");   hello.addSelectionListener(new SelectionAdapter(){public void widgetSelected(SelectionEvent e){       if(hello.getText().equals("Hello World!"))         hello.setText("Clicked");       else         hello.setText("Hello World!");}});menu=new Menu(shell);mi1=new MenuItem(menu,SWT.PUSH);mi1.setText("MenuItem 1");mi1.addSelectionListener(this);mi2=new MenuItem(menu,SWT.PUSH);mi2.setText("MenuItem 2");mi2.addSelectionListener(this);quit=new MenuItem(menu,SWT.PUSH);quit.setText("Quit");quit.addSelectionListener(this);//生成swt的traytray=display.getSystemTray();ti=new TrayItem(tray,0);ti.setToolTipText("This is a swt Tray!");ti.setImage(new Image(display,"E:\\My Documents\\My Pictures\\alm.gif"));//swt,tray的所有事件:ti.addSelectionListener(new SelectionListener(){//左键单击public void widgetSelected(SelectionEvent e){System.out.println("Tray Selcted");//左键单击时 ,隐藏,显示主窗口if(shell.isVisible()){shell.setVisible(false);}else{shell.setVisible(true);shell.forceActive();}}//左键双击,右键双击,都是它public void widgetDefaultSelected(SelectionEvent e){System.out.println("Tray widgetDefaultSelected");}});//右键单击,弹出菜单ti.addListener(SWT.MenuDetect,new Listener(){public void handleEvent(Event event){System.out.println("SWT.MenuDetect");menu.setLocation(display.getCursorLocation());       menu.setVisible(true);}});}public void start(){shell.open();while (!shell.isDisposed()) {if (!display.readAndDispatch())display.sleep();}display.dispose();}public static void main(String[] args) {HelloTray app=new HelloTray();app.start(); }public void widgetSelected(SelectionEvent e){System.out.println(e.getSource()+" selected.");if(e.getSource()==quit){System.out.println("Quit");display.dispose();System.exit(0);}}public void widgetDefaultSelected(SelectionEvent e){ }}

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备2023014753号-2
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网