Java中的鼠标事件
我正在尝试移动 JComponent(例如表格上的标签)。我正在使用 MouseMotionListener 的 mouseDragged 方法跟踪此事件。此方法完美地帮助我跟踪该项目。有没有办法在拖动完成后跟踪鼠标释放(.ie掉落事件)。
tktLabel1.addMouseMotionListener(new MouseMotionListener()
{
public void mouseDragged(MouseEvent arg0)
{
tktLabel1.setBounds(tktLabel1.getX() + arg0.getX(),
tktLabel1.getY() + arg0.getY(), width, height);
}
public void mouseMoved(MouseEvent arg0)
{
}
});
I am trying to move a JComponent say a label over a table.I am tracking this event using MouseMotionListener's mouseDragged method.This method perfectly helps me in tracking the item.Is there a way to track the mouse release after dragging is complete(.ie the dropping event).
tktLabel1.addMouseMotionListener(new MouseMotionListener()
{
public void mouseDragged(MouseEvent arg0)
{
tktLabel1.setBounds(tktLabel1.getX() + arg0.getX(),
tktLabel1.getY() + arg0.getY(), width, height);
}
public void mouseMoved(MouseEvent arg0)
{
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有 2 个鼠标事件监听器。您已经使用的 MouseMotionListener 和 MouseListener ,它监听诸如按下、释放等事件。
如果在这个接口上实现所有六个方法负担太大,您可以扩展 MouseAdapter 相反,它为所有事件类型提供默认的 no op 方法,您可以覆盖您需要的方法。
编辑
仔细检查后似乎发现 JList、JTable 和 JTree 需要一点额外的来支持拖放。您必须实现 DropTarget 收到这些事件的通知。
There are 2 listeners for mouse events. The MouseMotionListener which you are already using and the MouseListener, which listens for such things as pressed, released etc.
If it is too much of a burden to implement all six methods on this interface you can extend the MouseAdapter instead which provides default no op methods for all the event types and you can just override the ones you need.
EDIT
It seems on closer inspection that JList, JTable and JTree require a bit extra for drag and drop support. You will have to implement a DropTarget to be notified of these events.