0

我有一个JPopupMenu包含内部JMenu和分隔符的addSeparator(). 由于一些奇怪的处理,我在其中添加了一个MouseListenerJPopupMenu使其在mouseExited事件中不可见。这很好用,除了当鼠标试图越过分隔符时,它会触发事件(即使它JPopupMenu是超级组件)。

如果我删除该addSeparator()行,它会按预期工作。

有没有办法解决这个问题?还是我没有正确设置监听器?

代码如下:

JPopupMenu popupMenu = new JPopupMenu();
JMenu innerMenu = new JMenu("Inner");
// ... add JMenuItems
popupMenu.add(innerMenu);
popupMenu.addSeparator();
popupMenu.add(new JMenuItem("Exit"));

popupMenu.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseExited(MouseEvent e) {
        popupMenu.setVisible(false);
    }
});

完整的可编译示例

只需注释和取消注释该 popupMenu.addSeparator() 行即可注意到不同的行为

public class Test {


    public static void main(String[] args) throws Exception {
        if(!SystemTray.isSupported()) {
            throw new UnsupportedOperationException("SystemTray is not supported.");
        }

        final TrayIcon trayIcon = new TrayIcon(ImageIO.read(new File("resources/icon.gif")));
        final JPopupMenu popupMenu = new JPopupMenu();
        JMenu intervalMenu = new JMenu("Interval");
        ButtonGroup itemGroup = new ButtonGroup();

        JRadioButtonMenuItem oneSecondMenuItem = new JRadioButtonMenuItem("1 sec");
        itemGroup.add(oneSecondMenuItem);
        JRadioButtonMenuItem twoSecondMenuItem = new JRadioButtonMenuItem("2 sec");
        itemGroup.add(twoSecondMenuItem);

        intervalMenu.add(oneSecondMenuItem);
        intervalMenu.add(twoSecondMenuItem);

        popupMenu.add(intervalMenu);

        popupMenu.addSeparator();

        JMenuItem exitMenuItem = new JMenuItem("Exit");
        exitMenuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                SystemTray.getSystemTray().remove(trayIcon);
                System.exit(0);
            }
        });

        popupMenu.add(exitMenuItem);

        //Thanks to Artem Ananiev for this implementation idea
        //https://weblogs.java.net/blog/ixmal/archive/2006/05/using_jpopupmen.html
        trayIcon.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                if(e.getButton() == MouseEvent.BUTTON3) {
                    popupMenu.setLocation(e.getX() - 40, e.getY() - 40);
                    popupMenu.setInvoker(popupMenu);
                    popupMenu.setVisible(true);
                }
            }
        });

        popupMenu.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseExited(MouseEvent e) {
                popupMenu.setVisible(false);
            }
        });

        SystemTray.getSystemTray().add(trayIcon);
    }

}
4

1 回答 1

3

哇,您正在使用系统托盘图标。这些信息可能很重要。这就是为什么每个问题都应该发布 SSCCE。

无论如何,以下似乎有效:

if (! popupMenu.contains( e.getPoint() ) )
    popupMenu.setVisible(false);

编辑:

看起来问题在于 JSeparator 默认情况下不侦听 MouseEvents,因此所有鼠标事件都传递给其父级。因此,当您离开 JMenuItem 时,会为弹出菜单生成 mouseEntered() 事件,然后当您重新进入另一个 JMenuItem 时,会生成 mouseExited() 事件。

如果您为 JSeparator 启用 MouseEvents,那么看起来 JPopupMenu 没有收到该事件

//popupMenu.addSeparator();
popupMenu.add( new MySeparator() );
...


static class MySeparator extends JSeparator
{
    public MySeparator( )
    {
        super( JSeparator.HORIZONTAL );
        enableEvents(AWTEvent.MOUSE_EVENT_MASK);
    }

    /**
     * Returns the name of the L&F class that renders this component.
     *
     * @return the string "PopupMenuSeparatorUI"
     * @see JComponent#getUIClassID
     * @see UIDefaults#getUI
     */
    public String getUIClassID()
    {
        return "PopupMenuSeparatorUI";

    }
}
于 2013-10-24T20:22:29.040 回答