java swt最小化到托盘_SWT 中实现最小化到托盘图标,并只能通过托盘的弹出菜单关闭程序...
package com.unmi;
import org.eclipse.swt.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;
/**
* SWT 3.0 開始引入了 Tray,可以在系統欄放置你的程序圖標了
* 本程序實現的功能有四:
* 1. 點擊窗口的最小化或關閉按鈕都是隱藏窗口--任務欄里不顯示,不退出程序
* 2. 窗口隱藏時,任務欄無圖標,系統欄有圖標;窗口處于顯示狀態時則恰好相反
* 3. 窗口隱藏時可通過單擊系統欄圖標或點擊系統欄的 "顯示窗口" 菜單顯示窗口
* 4. 程序只能通過點擊系統欄的 "退出程序" 菜單項退出,窗口的 X 按鈕無效
* @author Unmi
*
*/
public class TrayExample {
public static void main(String[] args) {
Display display = new Display();
//禁用掉了最大化按鈕
final Shell shell = new Shell(display,SWT.SHELL_TRIM ^ SWT.MAX);
shell.setText("TrayExample");
//取系統中預置的圖標,省得測試運行時還得加個圖標文件
shell.setImage(display.getSystemImage(SWT.ICON_WORKING));
//構造系統欄控件
final Tray tray = display.getSystemTray();
final TrayItem trayItem = new TrayItem(tray, SWT.NONE);
//程序啟動時,窗口是顯示的,所以系統欄圖標隱藏
trayItem.setVisible(false);
trayItem.setToolTipText(shell.getText());
trayItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
toggleDisplay(shell, tray);
}
});
final Menu trayMenu = new Menu(shell, SWT.POP_UP);
MenuItem showMenuItem = new MenuItem(trayMenu, SWT.PUSH);
showMenuItem.setText("顯示窗口(&s)");
//顯示窗口,并隱藏系統欄中的圖標
showMenuItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
toggleDisplay(shell, tray);
}
});
trayMenu.setDefaultItem(showMenuItem);
new MenuItem(trayMenu, SWT.SEPARATOR);
//系統欄中的退出菜單,程序只能通過這個菜單退出
MenuItem exitMenuItem = new MenuItem(trayMenu, SWT.PUSH);
exitMenuItem.setText("退出程序(&x)");
exitMenuItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
shell.dispose();
}
});
//在系統欄圖標點擊鼠標右鍵時的事件,彈出系統欄菜單
trayItem.addMenuDetectListener(new MenuDetectListener(){
public void menuDetected(MenuDetectEvent e) {
trayMenu.setVisible(true);
}
});
trayItem.setImage(shell.getImage());
//注冊窗口事件監聽器
shell.addShellListener(new ShellAdapter() {
//點擊窗口最小化按鈕時,窗口隱藏,系統欄顯示圖標
public void shellIconified(ShellEvent e) {
toggleDisplay(shell, tray);
}
//點擊窗口關閉按鈕時,并不終止程序,而時隱藏窗口,同時系統欄顯示圖標
public void shellClosed(ShellEvent e) {
e.doit = false; //消耗掉原本系統來處理的事件
toggleDisplay(shell, tray);
}
});
shell.setSize(320, 240);
center(shell);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
/**
* 窗口是可見狀態時,則隱藏窗口,同時把系統欄中圖標刪除
* 窗口是隱藏狀態時,則顯示窗口,并且在系統欄中顯示圖標
* @param shell 窗口
* @param tray 系統欄圖標控件
*/
private static void toggleDisplay(Shell shell, Tray tray) {
try {
shell.setVisible(!shell.isVisible());
tray.getItem(0).setVisible(!shell.isVisible());
if (shell.getVisible()) {
shell.setMinimized(false);
shell.setActive();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 窗口居中顯示
* @param shell 要顯示的窗口
*/
private static void center(Shell shell){
Monitor monitor = shell.getMonitor();
Rectangle bounds = monitor.getBounds ();
Rectangle rect = shell.getBounds ();
int x = bounds.x + (bounds.width - rect.width) / 2;
int y = bounds.y + (bounds.height - rect.height) / 2;
shell.setLocation (x, y);
}
}
總結
以上是生活随笔為你收集整理的java swt最小化到托盘_SWT 中实现最小化到托盘图标,并只能通过托盘的弹出菜单关闭程序...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java内部类及其实例化
- 下一篇: Java静态内部类、匿名内部类、成员式内