gpt4 book ai didi

java - JPopupMenu 打开时开始鼠标拖动?

转载 作者:行者123 更新时间:2023-12-01 13:27:06 28 4
gpt4 key购买 nike

我有一个弹出菜单,当鼠标悬停在组件上时会打开该菜单。现在我希望能够在弹出窗口仍然打开时开始拖动组件,但弹出菜单关闭事件总是消耗第一次鼠标单击。有解决方法吗?

最佳答案

     public class A
.....
implements MouseListener, ActionListener
{
JPopupMenu pmnu ;
JMenuItem setcol ;

JList lst ;
Component cmp = null ;
int x = 0, y = 0;
...
private void
pop ( MouseEvent ev )
{
if ( ev.isPopupTrigger () )
{
cmp = ev.getComponent () ;
x = ev.getX () ;
y = ev.getY () ;
pmnu.show ( cmp, x, y ) ;
}
}
public void mouseDragged( MouseEvent ev )
{
pop ( ev ) ;
}
public void mouseReleased ( MouseEvent ev )
{
pop ( ev ) ;
}
public void actionPerformed( ActionEvent ev )
{
Object src = ev.getSource() ;

if ( src == setcol && cmp != null )
{
cmp.setBackground ( Color.yellow ) ;
return ;
}
}
public A( )
{
pmnu = new JPopupMenu () ;

setcol = new JMenuItem ( "Set color" ) ;

pmnu.add ( setcol ) ;
....
}

public void
init()
{
setcol.addActionListener ( this ) ;
....
lst.addMouseListener ( this ) ;
}
public void mouseClicked(MouseEvent ev) {}
public void mouseEntered(MouseEvent ev) {}
public void mouseExited(MouseEvent ev) {}
}

关于java - JPopupMenu 打开时开始鼠标拖动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21759838/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com