1

目前,当我右键单击 SystemTray 中的 TrayIcon 时,会显示 PopupMenu。但是,当我左键单击 TrayIcon 时,我希望它做同样的事情。

我想我可以通过在 TrayIcon 上使用 mouseListener 来完成此操作,但我不知道在 mouseClicked 事件中调用什么方法来实现所需的结果。

icon = new TrayIcon(img, tooltip, popup);

     icon.addMouseListener(
           new MouseAdapter() {
              public void mouseClicked(MouseEvent e) {
                 popup.setEnabled(true);
              }
           });

当我左键单击 TrayIcon 时,使用 setEnabled() 方法不会出现弹出菜单。它实际上没有明显的效果。我想知道我应该在 mouseClicked() 主体中使用什么方法,以便在左键单击时显示弹出窗口。

4

2 回答 2

8

基本上,在您的鼠标侦听器中,您需要确定按下了哪个按钮(以及可选的,多少次)。

关键的代码是这个......

if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 1) { ... }

我还包含了一些额外的代码,以确保弹出窗口不覆盖任务栏并显示在屏幕的可视区域内(这是我的一个 nit 选择;))

public class TestTrayIcon02 {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception ex) {
                }
                try {
                    final TrayIcon ti = new TrayIcon(ImageIO.read(getClass().getResource("/Smiley.png")), "Have a nice day");
                    final JPopupMenu popup = new JPopupMenu();

                    JMenuItem mi = new JMenuItem("Get me some");
                    mi.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            SystemTray.getSystemTray().remove(ti);
                            System.exit(0);
                        }
                    });

                    popup.add(mi);
                    ti.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseClicked(MouseEvent e) {
                            if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 1) {
                                Rectangle bounds = getSafeScreenBounds(e.getPoint());
                                Point point = e.getPoint();
                                int x = point.x;
                                int y = point.y;
                                if (y < bounds.y) {
                                    y = bounds.y;
                                } else if (y > bounds.y + bounds.height) {
                                    y = bounds.y + bounds.height;
                                }
                                if (x < bounds.x) {
                                    x = bounds.x;
                                } else if (x > bounds.x + bounds.width) {
                                    x = bounds.x + bounds.width;
                                }
                                if (x + popup.getPreferredSize().width > bounds.x + bounds.width) {
                                    x = (bounds.x + bounds.width) - popup.getPreferredSize().width;
                                }
                                if (y + popup.getPreferredSize().height > bounds.y + bounds.height) {
                                    y = (bounds.y + bounds.height) - popup.getPreferredSize().height;
                                }
                                popup.setLocation(x, y);
                                popup.setVisible(true);
                            }
                        }
                    });

                    SystemTray.getSystemTray().add(ti);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
    }

    public static Rectangle getSafeScreenBounds(Point pos) {

        Rectangle bounds = getScreenBoundsAt(pos);
        Insets insets = getScreenInsetsAt(pos);

        bounds.x += insets.left;
        bounds.y += insets.top;
        bounds.width -= (insets.left + insets.right);
        bounds.height -= (insets.top + insets.bottom);

        return bounds;

    }

    public static Insets getScreenInsetsAt(Point pos) {
        GraphicsDevice gd = getGraphicsDeviceAt(pos);
        Insets insets = null;
        if (gd != null) {
            insets = Toolkit.getDefaultToolkit().getScreenInsets(gd.getDefaultConfiguration());
        }
        return insets;
    }

    public static Rectangle getScreenBoundsAt(Point pos) {
        GraphicsDevice gd = getGraphicsDeviceAt(pos);
        Rectangle bounds = null;
        if (gd != null) {
            bounds = gd.getDefaultConfiguration().getBounds();
        }
        return bounds;
    }

    public static GraphicsDevice getGraphicsDeviceAt(Point pos) {

        GraphicsDevice device = null;

        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice lstGDs[] = ge.getScreenDevices();

        ArrayList<GraphicsDevice> lstDevices = new ArrayList<GraphicsDevice>(lstGDs.length);

        for (GraphicsDevice gd : lstGDs) {

            GraphicsConfiguration gc = gd.getDefaultConfiguration();
            Rectangle screenBounds = gc.getBounds();

            if (screenBounds.contains(pos)) {

                lstDevices.add(gd);

            }

        }

        if (lstDevices.size() > 0) {
            device = lstDevices.get(0);
        } else {
            device = ge.getDefaultScreenDevice();
        }

        return device;

    }
}
于 2013-02-04T06:33:54.257 回答
1

你试图做的显然是不可能的:

你不能PopupMenu用它的show方法来展示它,因为你需要指定 aJComponent但你TrayIcon不是一个(很奇怪,尽管它TrayIcon仍然可以做到,所以显然有一种方法,尽管不要问我..)。因此,正如 MadProgrammer 建议的那样,您应该尝试JPopupMenu改用。不要将它添加到您TrayIcon的,因为那是不可能的,而是JPopupMenu通过将 a 添加MouseListener到您的TrayIcon. 这应该够了吧:

final TrayIcon tray = new TrayIcon( img, tooltip, null);
final JPopupMenu menu = new JPopupMenu();
... // your menu initialization.
tray.addMouseListener( new MouseAdapter() {
    @Override
    public void mouseClicked(MouseEvent evt) {
        menu.setLocation( evt.getPoint() );
        menu.setVisible( true );
    }
}
于 2013-02-03T09:34:41.007 回答