swt tray demo:右鍵菜單,單擊隱藏,所有事件的例子

來源:互聯網
上載者:User
菜單
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){ }}


相關文章

Beyond APAC's No.1 Cloud

19.6% IaaS Market Share in Asia Pacific - Gartner IT Service report, 2018

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。