考虑到您的问题和评论中所说的内容,我会通过以下方式之一解决您的问题.
从技术上讲,你有两个选择:
1.每当用户将鼠标移动到弹出窗口之外时,隐藏弹出窗口.这样您就不会遇到用户点击的问题,因为弹出窗口会自动消失.
2.全局捕获此特定鼠标事件,如果弹出窗口可见,则在左键单击时使用该事件.我在下面的例子中展示了这个特定的解决方案
import java.awt.AWTEvent;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
public class DisableClickWhenPopupVisibleTest
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
final JPopupMenu popup = new JPopupMenu();
popup.add(new JMenu("aAaa"));
JPanel contentPane = new JPanel();
contentPane.add(popup);
JButton b = new JButton();
b.setAction(new AbstractAction("Button")
{
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e)
{
System.out.println("b actionPerformed");
}
});
contentPane.add(b);
contentPane.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e)
{
showPopup(e);
}
@Override
public void mouseReleased(MouseEvent e)
{
showPopup(e);
}
private void showPopup(MouseEvent e)
{
if(e.isPopupTrigger())
popup.show(e.getComponent(), e.getX(), e.getY());
}
});
//use global mouse event capture to disable left click on anything when popup is visible
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
@Override
public void eventDispatched(AWTEvent event)
{
MouseEvent me = (MouseEvent)event;
if(me.getID() == MouseEvent.MOUSE_PRESSED)
{
System.out.println("eventDispatched popup.vis="+popup.isVisible());
if( me.getButton() == MouseEvent.BUTTON3)
{
System.out.println("BUTTON3");
}
else if(me.getButton() == MouseEvent.BUTTON1)
{
System.out.println("BUTTON1");
if(popup.isVisible())
me.consume();
}
}
}
}, AWTEvent.MOUSE_EVENT_MASK);
JFrame f = new JFrame();
f.setContentPane(contentPane);
f.setSize(400, 300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
});
}
}
您可以通过右键单击按钮左侧稍微测试该示例,然后弹出窗口将显示.然后,如果单击按钮,则不会调用其操作.如果隐藏弹出窗口,则会正常调用该操作.此功能由以下代码行Toolkit.getDefaultToolkit().addAWTEventListener(…)提供.您可以注释掉该行,然后观察该动作将在您目前遇到的任何情况下发生.