JPopupMenu 行为

发布于 2024-08-06 10:03:40 字数 1935 浏览 2 评论 0原文

我在下面准备了一个小测试用例。我的问题是当我右键单击窗口时。 JPopupMenu 显示,但如果我单击 JWindow 菜单之外的任何位置都不会消失。我必须单击窗口上的某个位置才能将其删除,这不是预期的行为。

编辑: 阅读 akf 的答案后,我切换到 JFrame,当框架处于焦点并且弹出菜单显示时,当您单击另一个窗口时,它会消失。但如果窗口没有焦点并且您单击某处菜单不会消失。


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class test {

    static class window extends JWindow
    implements MouseListener, MouseMotionListener{

    JPopupMenu popMenu;
    JPanel panel = new JPanel();

    Point location;
    MouseEvent pressed;

    public window(){

        addMouseListener( this );
        addMouseMotionListener( this );

        JLabel label = new JLabel("JWindow", JLabel.CENTER);

        initPopMenu();
        add(label);
        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();
    }
}

I prepared a small test case below. My problem is when i right click on the window. JPopupMenu show up but if i click anywhere outside the JWindow menu does not disappear. I have to click somewhere on the window to get rid of it which is not the expected behavior.

EDIT:
after reading akf's answer i switched to JFrame, when frame is in focus and pop up menu is showing it disappears when you click on another window. but if the window does not have focus and you click somewhere menu does not disappear.


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class test {

    static class window extends JWindow
    implements MouseListener, MouseMotionListener{

    JPopupMenu popMenu;
    JPanel panel = new JPanel();

    Point location;
    MouseEvent pressed;

    public window(){

        addMouseListener( this );
        addMouseMotionListener( this );

        JLabel label = new JLabel("JWindow", JLabel.CENTER);

        initPopMenu();
        add(label);
        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技术交流群

发布评论

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

评论(3

无远思近则忧 2024-08-13 10:03:40

查看 JWindow.isFocusableWindow< 的 Java 文档/a>
JWindow 不能成为焦点窗口,除非它有所有者并且所有者可见。
您正在使用默认构造函数,因此您的 JWindow 具有共享所有者 asn 不可聚焦。当它不可聚焦时,当您单击其他位置时,它无法检测到焦点丢失。

我将 JWindow 更改为 JFrame 并在调用 setVisible 之前添加了对 setUndecorated(true); 的调用,它是为我工作。如果这些更改不适合您,请发布您正在使用的 Java 版本:java -fullversion

Take a look at the Java Doc for JWindow.isFocusableWindow
A JWindow cannot be the focused window unless it has an owner and the owner is visible.
You're using the default constructor, so your JWindow has the shared owner asn is not focusable. When it is not focusable, it cannot detect the loss of focus when you click somewhere else.

I changed JWindow to JFrame and added a call to setUndecorated(true); before the call to setVisible and it's working for me. If these changes do not make it work for you, please post the version of Java you are using: java -fullversion

囚我心虐我身 2024-08-13 10:03:40

在 Windows 上的 Java 6 中,我什至无法使用您提供的代码显示弹出窗口。另一方面,如果我将您的超类更改为 JFrame,它会按预期工作,当我单击窗口外部时,弹出窗口就会消失。您使用 JWindow 作为您的超类而不是 JFrame 是否有原因?如果您希望有一个无边框/无标题窗口,您可以在 JFrame 上调用 setUndecorated(true) (当然,在设置可见和打包之前。 )

In Java 6 on Windows, I cannot get the popup to even display with the code you have provided. On the other hand, if I change your superclass to JFrame, it works as desired, with the popup going away when I click outside of the window. Is there a reason why you are using JWindow as your superclass and not JFrame? If you wish to have a border-less/title-less window, you can call setUndecorated(true) on your JFrame (before you set visible and pack, of course.)

南渊 2024-08-13 10:03:40

如果菜单在 MouseExited 方法中可见,那么隐藏菜单怎么样?

What about hiding the menu if it's visible from within the MouseExited method?

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