to be a guide ,to be an elegant programmer

做一个优雅的码农!

博客园 首页 新随笔 联系 订阅 管理
    SWT编程中提供了Tray和TrayItem来生成系统状态栏图标。
但是,Tray不能直接实例化,需要调用系统的Display.getCurrent().getSystemTray(); 方法来获取Tray实例。
完整的生成TrayIcon的代码如下:
 1/**
 2     * 创建系统图标
 3     */

 4    protected void createTrayIcon()
 5    {
 6        Tray tray = Display.getCurrent().getSystemTray();
 7        TrayItem ti = new TrayItem(tray, SWT.PUSH);
 8        
 9        final Menu tmenu = new Menu(shell, SWT.POP_UP);
10        MenuItem tmenuItem = new MenuItem(tmenu, SWT.PUSH);
11        tmenuItem.setText("退出系统");
12        tmenuItem.addSelectionListener(new SelectionListener()
13        {
14            
15            public void widgetDefaultSelected(SelectionEvent e)
16            {
17                // TODO Auto-generated method stub
18                shell.close();
19                System.exit(0);
20            }

21            
22            
23            public void widgetSelected(SelectionEvent e)
24            {
25                // TODO Auto-generated method stub
26                shell.close();
27                System.exit(0);
28                
29            }

30            
31        }
);
32        ti.setImage(SWTResourceManager
33                .getImage(MainForm.class"/OmiuLogo.png"));
34        ti.setText("OmiuView DA ");
35        ti.addSelectionListener(new SelectionListener()
36        {
37            
38            public void widgetDefaultSelected(SelectionEvent e)
39            {
40                // TODO Auto-generated method stub
41                
42            }

43            
44            
45            public void widgetSelected(SelectionEvent e)
46            {
47                // TODO Auto-generated method stub
48                
49                shell.setVisible(true);
50                shell.forceActive();
51                
52            }

53            
54        }
);
55        
56        
57        // 处理双击事件
58        ti.addListener(SWT.MenuDetect, new Listener()
59        {
60            
61            public void handleEvent(Event event)
62            {
63                // TODO Auto-generated method stub
64                tmenu.setLocation(Display.getCurrent().getCursorLocation());
65                tmenu.setVisible(true);
66            }

67            
68        }
);
69        ToolTip toolTip = new ToolTip(shell, SWT.None);
70        toolTip.setAutoHide(true);
71        toolTip.setText("退出");
72        ti.setToolTip(toolTip);
73    }
posted on 2007-07-23 14:44  老苏  阅读(924)  评论(0)    收藏  举报