JPopupMenu 行为
我在下面准备了一个小测试用例。我的问题是当我右键单击窗口时。 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
查看 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
toJFrame
and added a call tosetUndecorated(true);
before the call tosetVisible
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
在 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 notJFrame
? If you wish to have a border-less/title-less window, you can callsetUndecorated(true)
on yourJFrame
(before you set visible and pack, of course.)如果菜单在
MouseExited
方法中可见,那么隐藏菜单怎么样?What about hiding the menu if it's visible from within the
MouseExited
method?