如何设置 JPopupMenu 相对于系统托盘图标的位置以及何时或如何关闭?

How to set location of JPopupMenu relative to system tray icon and when or how to close?

我正在使用 JPopupMenu 作为 SystemTray 弹出菜单,但第一个问题是如何确定放置弹出菜单的最安全位置?

第二个如何或何时停止显示弹出菜单? 我看到了 this post 但是我看不懂代码。

这是我的示例代码:


import java.awt.AWTException;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;

public class SampleTrayIcon implements MouseListener, MouseMotionListener {
    /**
     * System Tray
     */
    private final SystemTray tray = SystemTray.getSystemTray();

    /**
     * Note Tray icon
     */
    private final TrayIcon trayIcon;

    /**
     * Note Tray icon popup menu
     */
    private final JPopupMenu popUpMenu = new JPopupMenu();

    /**
     * constructor
     */
    public SampleTrayIcon() {
        // check system tray is supported
        if (!SystemTray.isSupported()) {
            System.err.println("System tray is not supported");
            System.exit(1);
        }

        // Create a TrayIcon image
        BufferedImage image = null;

        // try to read image from resources
        try {
            image = ImageIO.read(new URL("https://i.stack.imgur.com/lM3aS.png"));
        } catch (Exception e) {
            System.err.println("Failed to read image from resources");
            System.exit(1);
        }

        // Create a TrayIcon
        trayIcon = new TrayIcon(image, "QuickNote");

        // set icon auto resize
        trayIcon.setImageAutoSize(true);

        // Create Menu items
        var viewOnHover = new JCheckBoxMenuItem("onHover");
        var aboutMenuItem = new JMenuItem("About");
        var exitMenuItem = new JMenuItem("Exit");

        // add Menuitems to PopUpMenu
        popUpMenu.add(viewOnHover);
        popUpMenu.addSeparator();
        popUpMenu.add(aboutMenuItem);
        popUpMenu.add(exitMenuItem);

        // add mouse listener to tray icon
        trayIcon.addMouseListener(this);
        trayIcon.addMouseMotionListener(this);

        // add tray to system
        try {
            tray.add(trayIcon);
        } catch (AWTException e) {
            System.err.println("TrayIcon could not be added to system tray");
            System.exit(1);
        }
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        // TODO Auto-generated method stub
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        // TODO Auto-generated method stub
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if(SwingUtilities.isRightMouseButton(e)) {
            System.out.println(e.getPoint());
            popUpMenu.setLocation(e.getPoint());
            popUpMenu.setInvoker(popUpMenu);
            popUpMenu.setVisible(true);
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub
    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub
    }

    public static void main(String[] args) {
        new SampleTrayIcon();
    }
}

完整回购请访问Github

上面的输出如下

第一个,我需要一些建议来创建一个算法来将 Jpopupmenu 放置在正确的位置这个也有助于将未修饰的 UTILITY JFrame 放置在托盘图标附近。

第二个是 Jpopupmenu 放置在位置后应该如何或何时设置为 false。

您正在创建一个新的 JPopupMenu 以在单击时显示,但您可以改为使用 TrayIcon 的内置菜单。如果您使用 PopupMenu 而不是 JPopupMenu,它应该显示在正确的位置。

PopupMenu popup = new PopupMenu();
TrayIcon trayIcon = new TrayIcon(createImage("images/bulb.gif", "tray icon"));
MenuItem aboutItem = new MenuItem("About");
popup.add(aboutItem);
trayIcon.setPopupMenu(popup);

https://docs.oracle.com/javase/tutorial/uiswing/misc/systemtray.html

https://docs.oracle.com/javase/7/docs/api/java/awt/TrayIcon.html#setPopupMenu(java.awt.PopupMenu)