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);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值