//注?窗口??
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();
}
/**
* 窗口是可????,??藏窗口,在托?中?示程序??
* 窗口是?藏???,??示窗口,?托?中???除
*/
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();
}
}
/**
* 窗口居中?示
*/
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);
}
}