`
samsongbest
  • 浏览: 167847 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

SWT 系统托盘

    博客分类:
  • SWT
 
阅读更多

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.events.ShellListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.internal.win32.OS;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tray;
import org.eclipse.swt.widgets.TrayItem;

public class SystemTray extends Shell {

    /**
    * Launch the application
    *
    * @param args
    */
    public static void main(String args[]) {
        try {
            Display display = Display.getDefault();
            SystemTray shell = new SystemTray(display, SWT.SHELL_TRIM);
            shell.createSystemTray(shell);
            shell.open();
            shell.layout();
            while (!shell.isDisposed()) {
                if (!display.readAndDispatch())
                    display.sleep();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
    * Create the shell
    *
    * @param display
    * @param style
    */
    public SystemTray(Display display, int style) {
        super(display, style);
        createContents();
    }

    /**
    * Create contents of the window
    */
    protected void createContents() {
        setText("SWT Application");
        setSize(500, 375);

    }

    protected void checkSubclass() {
        // Disable the check that prevents subclassing of SWT components
    }

    public void createSystemTray(final Shell shell) {
        // 下面两句的效果是:在任务栏不显示
        final int hWnd = shell.handle;
        OS.SetWindowLong(hWnd, OS.GWL_EXSTYLE, OS.WS_EX_CAPTIONOKBTN);
        shell.addShellListener(new ShellListener() {
            public void shellDeactivated(org.eclipse.swt.events.ShellEvent e) {
                System.out.println("shellDeactivated");
            }

            public void shellActivated(org.eclipse.swt.events.ShellEvent e) {
                System.out.println("shellActivated");
            }

            public void shellClosed(org.eclipse.swt.events.ShellEvent e) {
                System.out.println("shellClosed");
            }

            /**
            * Sent when a shell is un-minimized.
            */
            public void shellDeiconified(org.eclipse.swt.events.ShellEvent e) {
                System.out.println("shellDeiconified");
            }

            /**
            * Sent when a shell is minimized. Shell 最小化后事件
            */
            public void shellIconified(org.eclipse.swt.events.ShellEvent e) {
                // 最小化时不显示在任务栏
                shell.setVisible(false);
                System.out.println("shell.setVisible(false);");
            }

        });

        Display display = shell.getDisplay();
        // Image image = new Image (display, 16, 16);
        Image image = new Image(display, "images/toc_closed.gif");
        final Tray tray = display.getSystemTray();
        final TrayItem trayItem = new TrayItem(tray, SWT.NONE);
        trayItem.setToolTipText("SWT TrayItem");
        trayItem.addListener(SWT.Show, new Listener() {
            public void handleEvent(Event event) {
                System.out.println("show");
            }
        });
        trayItem.addListener(SWT.Hide, new Listener() {
            public void handleEvent(Event event) {
                System.out.println("hide");
            }
        });

        // 托盘单击事件
        trayItem.addListener(SWT.Selection, new Listener() {

            public void handleEvent(Event event) {
                System.out.println("selection");
                // shell.setVisible(true);
                // shell.setMinimized(false);
            }
        });

        // 托盘双击事件
        trayItem.addListener(SWT.DefaultSelection, new Listener() {
            public void handleEvent(Event event) {
                System.out.println("default selection");
                shell.setVisible(true);
                shell.setMinimized(false);
            }
        });

        // 添加托盘右键菜单
        final Menu menu = new Menu(shell, SWT.POP_UP);

        MenuItem menuItemMaximize = new MenuItem(menu, SWT.PUSH);// 最大化菜单
        menuItemMaximize.setText("Maximize");
        menuItemMaximize.addSelectionListener(new SelectionListener() {
            public void widgetSelected(SelectionEvent e) {
                shell.setVisible(true);
                shell.setMaximized(true);
            }

            public void widgetDefaultSelected(SelectionEvent e) {
                // widgetSelected(e);
            }

        });

        MenuItem menuItemMinimize = new MenuItem(menu, SWT.PUSH);// 最小化菜单
        menuItemMinimize.setText("Minimize");
        menuItemMinimize.addSelectionListener(new SelectionListener() {
            public void widgetSelected(SelectionEvent e) {
                shell.setMinimized(true);
            }

            public void widgetDefaultSelected(SelectionEvent e) {
                // widgetSelected(e);
            }
        });

        new MenuItem(menu, SWT.SEPARATOR);// 分割条

        MenuItem menuItemClose = new MenuItem(menu, SWT.PUSH);// 关闭菜单
        menuItemClose.setText("Close");
        menuItemClose.addSelectionListener(new SelectionListener() {
            public void widgetDefaultSelected(SelectionEvent e) {
                Display.getCurrent().close();
            }

            public void widgetSelected(SelectionEvent e) {
                widgetDefaultSelected(e);
            }
        });

        trayItem.addListener(SWT.MenuDetect, new Listener() {
            public void handleEvent(Event event) {
                System.out.println("shell.isVisible():" + shell.isVisible());
                menu.setVisible(true);
                if (shell.isVisible()) {
                    menu.getItem(0).setEnabled(true);
                    menu.getItem(1).setEnabled(true);
                } else {
                    menu.getItem(0).setEnabled(true);
                    menu.getItem(1).setEnabled(false);
                }
            }
        });
        trayItem.setImage(image);

    }

}

分享到:
评论

相关推荐

    java创建系统托盘实现(swt和jdk6分别实现)

    trayItem.setToolTip("这是一个SWT系统托盘示例"); trayItem.addListener(SWT.Selection, event -> { System.out.println("托盘图标被点击"); }); trayItem.setVisible(true); } else { System.err.println...

    Swing 实现系统托盘

    在Java的Swing库中,实现系统托盘功能可以为应用程序提供一个持续的交互界面,即使主窗口已经最小化或关闭。系统托盘,也称为通知区域或系统通知区,通常位于操作系统任务栏的右侧,允许用户快速访问常用的功能或...

    最小化到系统托盘java

    ### 最小化到系统托盘Java实现解析 在日常软件开发过程中,经常会有将应用程序窗口最小化至系统托盘的需求,这种设计不仅节省了屏幕空间,还为用户提供了一种更为便捷的操作方式。本文将深入探讨如何使用Java实现这...

    java swt 隐藏和显示任务栏图标和托盘图标(2)

    `Tray`代表系统托盘区域,`TrayItem`是托盘区内的图标。下面是如何创建和管理托盘图标的示例: ```java Tray tray = display.getSystemTray(); if (tray != null) { Image image = new Image(display, "path_to_...

    RCP系统托盘实现方法

    RCP(Rich Client Platform)系统托盘实现方法主要涉及到如何将程序最小化到Windows操作系统的系统托盘区域,以便在不占用桌面空间的同时,让用户能够快速访问和控制应用程序。以下是一份详细的步骤介绍: 1. **...

    *此源码已更新并重新上传java swt 隐藏和显示任务栏图标和托盘图标

    在实际项目中,使用SWT进行这类操作可以提升用户体验,允许用户在不关闭应用程序的情况下将其最小化到系统托盘,从而节省任务栏空间。此外,通过控制图标可见性,可以实现如后台运行、定时提醒等高级功能。对于...

    SWT-Win32-Extension

    2. **系统托盘图标**:扩展支持在Windows任务栏创建系统托盘图标,允许程序在后台运行,并提供通知和快捷菜单,增强了应用程序的可访问性和互动性。 3. **高级控件**:SWT-Win32-Extension 添加了一些Windows特有的...

    Eclipse SWT 开发参考

    - **Tray组件**:用于创建系统托盘图标。 - **Tree组件**:用于创建树形结构的列表。 - **Table组件**:用于创建表格形式的数据展示。 - **TabFolder组件**:用于创建带标签页的容器。 - **MessageBox组件**:用于...

    swt 隐藏显示任务栏核心

    SWT 3.0 开始引入了 Tray,可以在系统栏放置你的程序图标了 本程序实现的功能有四: * 1. 点击窗口的最小化或关闭按钮都是隐藏窗口--任务栏里不显示,不退出程序 * 2. 窗口隐藏时,任务栏无图标,系统栏有图标;...

    SWT开发参考文档

    - **轻量级封装**:SWT实现了Java GUI库的功能封装,使得开发者能够轻松地创建具有本地操作系统外观的用户界面。 - **跨平台支持**:尽管其名称中有“标准”一词,但SWT实际上支持多个平台,包括Windows、Linux和Mac...

    SWT疑难点解答,帮助新手朋友

    Tray是SWT提供的系统托盘类,允许在系统托盘区域添加图标。然而,需要注意的是,Tray的跨平台支持并不完全,因此在实际开发中应谨慎使用。 Table控件是SWT中用于展示和编辑大量数据的重要组件。创建Table并添加列和...

    swt-api-eclipse3.6

    - SWT API允许开发者访问操作系统的一些特定功能,如剪贴板、注册表、系统托盘图标等。 通过深入理解和熟练运用SWT API,开发者能够在Eclipse 3.6环境中构建出功能丰富、性能高效、用户体验良好的桌面应用。"swt-...

    JFace_SWT.doc

    - **Tray**: 系统托盘,可以在任务栏中显示一个小图标。 #### 六、容器类 SWT 中的容器类用于组织和管理其他组件,常见的容器包括: - **Composite**: 最基本的容器类,可以包含其他组件。 - **Group**: 分组容器...

    【重大更新】用SWT/JFace实现的放大镜JZoomer V1.1.1(附源码)

    实现功能: ·字符串国际化 ·系统文件可编辑 ·系统托盘显示 ·支持滚轮缩放功能 ·控件拖拽功能 <br>20071227 1. JExplorer实现,初步了解JFace框架的设计思想,了解Action的使用...

    jdic的插件

    通过这些文件,开发者可以学习如何在Java项目中使用JDIC来创建具有本地系统特性的应用程序,如系统托盘图标,以及如何将JDIC与SWT结合,以便在保持跨平台性的同时,充分利用特定操作系统的功能。同时,变更日志和...

    Eclipse_Swt_Jface_核心应用_部分19

    9.5 系统托盘(Tray和TrayItem) 164 9.6 滑动组件 167 9.6.1 滑块(Slider) 167 9.6.2 刻度条(Scale) 168 9.6.3 微调按钮(Spinner) 168 9.7 进度条(ProgressBar) 169 9.8 对话框 170 9.8.1 ...

    [开源]用SWT/JFace实现的放大镜软件jZoomer v1.2.0(附源码)

    实现功能: ·字符串国际化 ·系统文件可编辑 ·系统托盘显示 ·支持滚轮缩放功能 ·控件拖拽功能 20071227 1. JExplorer实现,初步了解JFace框架的设计思想,了解Action的使用方法...

    [原创]用SWT/JFace实现的放大镜软件JZoomer(附源码)

    实现功能: ·字符串国际化 ·系统文件可编辑 ·系统托盘显示 ·支持滚轮缩放功能 ·控件拖拽功能 20071227 1. JExplorer实现,初步了解JFace框架的设计思想,了解Action的使用方法...

    mp3播放器java 1.02版

    3:更正了系统托盘菜单中的播放模式显示不能与播放器的播放模式同步的BUG。 4:在系统托盘菜单中加入音量控制功能,每次改变音量的 5% 。 5:修正了保存列表和打开列表功能. (注意:在保存列表的时候一定...

Global site tag (gtag.js) - Google Analytics