本文介绍了JPopupMenu行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面准备了一个小测试案例。我的问题是当我右键单击窗口时。 JPopupMenu出现但如果我点击JWindow菜单外的任何地方都不会消失。我必须点击窗口上的某个地方才能摆脱它,这不是预期的行为。

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.

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

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


推荐答案

Take查看Java Doc for
除非拥有所有者且所有者可见,否则JWindow不能成为焦点窗口。
您正在使用默认构造函数,因此您的JWindow具有共享所有者asn不可聚焦。当它不可聚焦时,当你点击其他地方时,它无法检测到失去焦点。

Take a look at the Java Doc for JWindow.isFocusableWindowA 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.

我改变了 JWindow JFrame 并在调用 setVisible setUndecorated(true); 的调用/ code>,它对我有用。如果这些更改不适合您,请发布您正在使用的Java版本: java -fullversion

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

这篇关于JPopupMenu行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 23:23