JPopupMenu行为OSX 10.6.7

时间:2011-07-23 09:55:40

标签: java swing macos jpopupmenu

我的问题是当我右键单击下面示例中的JFrame时,JPopupMenu会显示,但如果我点击JFrame外的任何地方,菜单就不会消失。我必须点击JFrame上的某个地方来摆脱它,这不是预期的行为。以下是重现的步骤:

  1. 从Eclipse运行窗口类(出现JFrame)
  2. 单击Eclipse工作区(JFrame失去焦点并隐藏在eclipse后面)
  3. 最小化Eclipse(出现JFrame)
  4. 将鼠标移到JFrame上并右键单击(弹出窗口显示)
  5. 点击某处(不是JFrame或Popup)。弹出窗口不会消失
  6. 我正在运行OS X 10.6.7和Java完整版1.6.0_24-b07-334

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    class test
    {
    
    static class window extends JFrame implements MouseListener,
            MouseMotionListener
    {
    
        JPopupMenu popMenu;
        JPanel panel = new JPanel();
    
        Point location;
        MouseEvent pressed;
    
        public window()
        {
    
            addMouseListener(this);
            addMouseMotionListener(this);
    
            JLabel label = new JLabel("JFrame", JLabel.CENTER);
    
            initPopMenu();
            add(label);
            setUndecorated(true);
            setVisible(true);
    
            // setAlwaysOnTop(true);
            setLocationRelativeTo(null);
            pack();
        }
    
        public void initPopMenu()
        {
            popMenu = new JPopupMenu();
            JMenuItem item;
    
            item = new JMenuItem("Title");
            item.setEnabled(false);
            popMenu.add(item);
            popMenu.addSeparator();
    
            item = new JMenuItem("Item One");
            popMenu.add(item);
    
            item = new JMenuItem("Item 2");
            popMenu.add(item);
    
            item = new JMenuItem("Item 3");
            popMenu.add(item);
        }
    
        public void mousePressed(MouseEvent e)
        {
            pressed = e;
            int nModifier = e.getModifiers();
            if (((nModifier & InputEvent.BUTTON2_MASK) != 0)
                    || ((nModifier & InputEvent.BUTTON3_MASK) != 0))
                popMenu.show( this, e.getX(), e.getY() );
        }
    
        public void mouseClicked(MouseEvent e)
        {
        }
    
        public void mouseReleased(MouseEvent e)
        {
        }
    
        public void mouseDragged(MouseEvent me)
        {
        }
    
        public void mouseMoved(MouseEvent e)
        {
        }
    
        public void mouseEntered(MouseEvent e)
        {
        }
    
        public void mouseExited(MouseEvent e)
        {
        }
    }
    
    public static void main(String[] args)
    {
        window dw = new window();
    }
    }
    

1 个答案:

答案 0 :(得分:0)

你可以添加一个窗口Focus Listener,当窗口失去焦点时隐藏菜单

        this.addWindowFocusListener(new WindowFocusListener() {
            @Override
            public void windowLostFocus(WindowEvent e) {
                if(popMenu != null){
                    popMenu.setVisible(false);
                }
            }
            @Override
            public void windowGainedFocus(WindowEvent e) {
                //System.out.println(e);
            }
        });