JPopupMenu 行为 OSX 10.6.7

发布于 2024-11-26 04:26:27 字数 2200 浏览 1 评论 0原文

我的问题是,当我右键单击下面示例中的 JFrame 时,会显示 JPopupMenu,但如果我单击 JFrame 之外的任何位置,菜单不会消失。我必须单击 JFrame 上的某个位置才能将其删除,这不是预期的行为。以下是重现的步骤:

  1. 从 Eclipse 运行窗口类(出现 JFrame)
  2. 单击进入 Eclipse 工作区(JFrame 失去焦点并隐藏在 Eclipse 后面)
  3. 最小化 Eclipse(出现 JFrame)
  4. 将鼠标悬停在 JFrame 上并右键单击(弹出窗口出现)
  5. 单击某处(不在 JFrame 或弹出窗口中)。弹出窗口不会消失

我正在运行 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();
}
}

My problem is when I right click on the JFrame in the example below, the JPopupMenu shows up but if I click anywhere outside the JFrame, the menu does not disappear. I have to click somewhere on the JFrame to get rid of it which is not the expected behavior. Here are the steps to reproduce:

  1. Run the window class from Eclipse (JFrame appears)
  2. Click into the Eclipse workspace (JFrame loses focus and is hidden behind eclipse)
  3. Minimize Eclipse (JFrame appears)
  4. Go with your mouse over JFrame and make a right click (Popup appears)
  5. Click somewhere (Not into JFrame or Popup). The Popup will not disappear

I'm running OS X 10.6.7 and Java full version 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();
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风追烟花雨 2024-12-03 04:26:27

您可以添加一个windowFocusListener,当窗口失去焦点时隐藏菜单

        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);
            }
        });

you can add a windowFocusListener, hide the menu when window lost focus

        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);
            }
        });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文