欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SWT在任务栏上显示 博客分类: java SWT托盘显示资源文件取得 

程序员文章站 2024-03-19 09:55:10
...
package crtjunit;

import java.io.InputStream;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
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;

import com.jia.dialog.SelectFileDlg;

public class MainClass {

    public static void main(String[] args) {
        InputStream path=MainClass.class.getResourceAsStream("logo.gif");

        Display display = new Display();
        final Shell shell = new Shell(display);

        Image image = new Image(display, path);

        Tray tray = display.getSystemTray();
        if (tray != null) {
            final TrayItem item = new TrayItem(tray, SWT.NONE);
            item.setToolTipText("TEST");
            item.addListener(SWT.Show, new Listener() {
                public void handleEvent(Event event) {
                }
            });

            item.addListener(SWT.Hide, new Listener() {
                public void handleEvent(Event event) {
                }
            });

            item.addListener(SWT.Selection, new Listener() {
                public void handleEvent(Event event) {
                }
            });

            final Menu menu = new Menu(shell, SWT.POP_UP);
            MenuItem mi = new MenuItem(menu, SWT.PUSH);
            mi.setText("close");
            mi.addListener(SWT.Selection, new Listener() {
                public void handleEvent(Event event) {
                    shell.close();
                }
            });

            item.addListener(SWT.MenuDetect, new Listener() {
                public void handleEvent(Event event) {
                    menu.setVisible(true);
                }
            });

            item.setImage(image);
        }

        SelectFileDlg test = new SelectFileDlg(shell);
        test.setBlockOnOpen(true);
        test.open();
    }
}