2

我正在使用 SWT 开发一个 OSX 应用程序,并且系统托盘中有一个 TrayIcon。我让它工作了,但是有一件小事真的让我很烦恼。当您右键单击该图标时,上下文菜单将按照我的意图显示。

(右键点击)

但是,当我左键单击时,它只会弹出菜单。

(左键单击)

如何使左键单击的行为与右键一样?

两者的代码几乎相同:

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

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

如果有人想查看整个项目的代码:

import org.eclipse.swt.SWT;
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 org.eclipse.wb.swt.SWTResourceManager;

public class Icon {


public static void main(String[] args) {
Display display = new Display ();
Shell shell = new Shell (display);
shell.setVisible(false);


final Tray tray = display.getSystemTray ();
if (tray == null) {
    System.out.println ("The system tray is not available");
} else {
    final TrayItem item = new TrayItem (tray, SWT.NONE);
    item.setToolTipText("Test Tool Tip");

    final Menu menu = new Menu (shell, SWT.POP_UP);

    MenuItem mi = new MenuItem (menu, SWT.PUSH);
    mi.setText ("View Messages");
    mi.addListener (SWT.Selection, new Listener () {
        public void handleEvent (Event event) {
            System.out.println("Messages will be displayed.");
        }
    });


    new MenuItem(menu, SWT.SEPARATOR);

    MenuItem mi1 = new MenuItem (menu, SWT.PUSH);
    mi1.setText ("Help");
    mi1.addListener (SWT.Selection, new Listener () {
        public void handleEvent (Event event) {
            System.out.println("Help will be displayed.");
        }
    });

    MenuItem mi2 = new MenuItem (menu, SWT.PUSH);
    mi2.setText ("Version 1.0");

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


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

    item.addListener (SWT.MenuDetect, new Listener () {
        public void handleEvent (Event event) {
            menu.setVisible (true);
        }
    });
    item.setImage(SWTResourceManager.getImage(Icon.class, "/main/black.png"));
}

while (!shell.isDisposed ()) {
    if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}
4

1 回答 1

2

如果您只添加 this( 并删除 SWT.DefaultSelection 和 SWT.Selection 部分) ,这将按预期工作

item.addListener (SWT.MenuDetect, new Listener () {

                                @Override
                                public void handleEvent(
                                        org.eclipse.swt.widgets.Event event) {
                                    menu.setVisible (true);



                                }
                            });
于 2014-06-24T08:56:21.500 回答