java SWT界面的托盘显示

  1. //点击关闭或者最小化时程序不会退出而是显示到托盘中    当然也可以是显示在任务栏中的 
  2. public class TrayApp {   
  3.     public static void main(String[] args) {   
  4.         Display display = new Display();   
  5.            
  6.         final Shell shell = new Shell(display);   
  7.         shell.setText("最小化到系統托盤");   
  8.            
  9.         //取消系統中預設的圖標,預設圖標在托盤不能顯示   
  10.         shell.setImage(display.getSystemImage(SWT.ICON_INFORMATION));   
  11.            
  12.         //構建系統托盤   
  13.         final Tray tray = display.getSystemTray();   
  14.         final TrayItem trayItem = new TrayItem(tray, SWT.NONE);   
  15.         //設置在托盤中顯示的程序圖標   
  16.         trayItem.setImage(display.getSystemImage(SWT.ICON_INFORMATION));   
  17.            
  18.         //程序啟動時,窗口是顯示的,所以托盤圖標隱藏   
  19.         trayItem.setVisible(false);   
  20.         trayItem.setToolTipText(shell.getText());   
  21.            
  22.         trayItem.addSelectionListener(new SelectionAdapter() {   
  23.             public void widgetSelected(SelectionEvent e) {   
  24.                 toggleDisplay(shell, tray);   
  25.             }   
  26.         });   
  27.            
  28.         final Menu trayMenu = new Menu(shell, SWT.POP_UP);   
  29.         MenuItem showMenuItem = new MenuItem(trayMenu, SWT.PUSH);   
  30.         showMenuItem.setText("顯示窗口(&s)");   
  31.            
  32.         //顯示窗口,并隱藏托盤圖標   
  33.         showMenuItem.addSelectionListener(new SelectionAdapter() {   
  34.             public void widgetSelected(SelectionEvent e) {   
  35.                 toggleDisplay(shell, tray);   
  36.             }   
  37.         });   
  38.            
  39.         trayMenu.setDefaultItem(showMenuItem);   
  40.            
  41.         new MenuItem(trayMenu, SWT.SEPARATOR);   
  42.            
  43.         //托盤中的退出菜單,程式只能通過這個菜單退出   
  44.         MenuItem exitMenuItem = new MenuItem(trayMenu, SWT.PUSH);   
  45.         exitMenuItem.setText("退出程式(&x)");   
  46.            
  47.            
  48.         exitMenuItem.addSelectionListener(new SelectionAdapter() {   
  49.             public void widgetSelected(SelectionEvent event) {   
  50.                 shell.dispose();   
  51.             }   
  52.         });   
  53.            
  54.         //在托盤圖標點擊鼠標右鍵時的事件,彈出系統菜單   
  55.         trayItem.addMenuDetectListener(new MenuDetectListener() {   
  56.             public void menuDetected(MenuDetectEvent e) {   
  57.                 trayMenu.setVisible(true);   
  58.             }   
  59.         });   
  60.            
  61.         //注冊窗口監聽   
  62.         shell.addShellListener(new ShellAdapter() {   
  63.             //點擊窗口最小化按鈕時,窗口隱藏,托盤中顯示圖標   
  64.             public void shellIconified(ShellEvent e) {   
  65.                 toggleDisplay(shell, tray);   
  66.             }   
  67.                
  68.             //點擊窗口關閉時,并不終止程序,而是隱藏窗口,同時托盤中顯示圖標   
  69.             public void shellClosed(ShellEvent e) {   
  70.                 e.doit = false//取消關閉操作  
  71.                 toggleDisplay(shell, tray);   
  72.             }   
  73.         });   
  74.            
  75.         shell.setSize(320240);   
  76.         center(shell);   
  77.         shell.open();   
  78.         while(!shell.isDisposed()) {   
  79.             if(!display.readAndDispatch()) {   
  80.                 display.sleep();   
  81.             }   
  82.         }   
  83.         display.dispose();   
  84.     }   
  85.        
  86.     /**  
  87.      * 窗口是可見狀態時,則隱藏窗口,在托盤中顯示程序圖標  
  88.      * 窗口是隱藏狀態時,則顯示窗口,將托盤中圖標刪除  
  89.      */  
  90.     private static void toggleDisplay(Shell shell, Tray tray) {   
  91.         try {   
  92.             shell.setVisible(!shell.isVisible());           //控制窗口顯示  
  93.             tray.getItem(0).setVisible(!shell.isVisible()); //控制托盤圖標顯示  
  94.             //如果窗口是顯示狀態   
  95.             if(shell.getVisible()) {   
  96.                 shell.setMinimized(false);  //阻止窗口最小化  
  97.                 shell.setActive();          //激活窗口   
  98.             }   
  99.         } catch(Exception e) {   
  100.             e.printStackTrace();   
  101.         }   
  102.     }   
  103.        
  104.     /**  
  105.      * 窗口居中顯示  
  106.      */  
  107.     private static void center(Shell shell) {   
  108.         Monitor monitor = shell.getMonitor();   
  109.         Rectangle bounds = monitor.getBounds();   
  110.         Rectangle rect = shell.getBounds();   
  111.         int x = bounds.x + (bounds.width - rect.width)/2;   
  112.         int y = bounds.y + (bounds.height - rect.height)/2;   
  113.         shell.setLocation(x, y);   
  114.     }   
  115. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值