在Java中,如何在按下鼠标按钮时侦听鼠标移动事件
我的程序中的鼠标事件有问题。我正试图用画布编写一个绘图程序 如果用户左键单击并移动鼠标,则应进行绘制。因此,我定义了一个类在Java中,如何在按下鼠标按钮时侦听鼠标移动事件,java,mouseevent,mouse,mousemove,Java,Mouseevent,Mouse,Mousemove,我的程序中的鼠标事件有问题。我正试图用画布编写一个绘图程序 如果用户左键单击并移动鼠标,则应进行绘制。因此,我定义了一个类Drawer,其中包含一个boolean allow\u draw,并添加了一个方法draw 使用画布中的mousemoved事件调用draw,并使用mousepressed和released将allow\u draw设置为true和false 但是,当我按下鼠标按钮时,mousemoved没有启动 我的问题是:当按下鼠标按钮时,我如何能听到鼠标的移动 希望你知道我在找什么:
Drawer
,其中包含一个boolean allow\u draw
,并添加了一个方法draw
使用画布中的mousemoved
事件调用draw
,并使用mousepressed
和released
将allow\u draw
设置为true和false
但是,当我按下鼠标按钮时,mousemoved
没有启动
我的问题是:当按下鼠标按钮时,我如何能听到鼠标的移动
希望你知道我在找什么:)你能发布你的源代码吗?请尝试添加MouseMotionListener。下面是我正在从事的一个项目的一个例子
addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseDragged(java.awt.event.MouseEvent evt) {
formMouseDragged(evt);
}
public void mouseMoved(java.awt.event.MouseEvent evt) {
formMouseMoved(evt);
}
});`
你能发布你的源代码吗?请尝试添加MouseMotionListener。下面是我正在从事的一个项目的一个例子
addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseDragged(java.awt.event.MouseEvent evt) {
formMouseDragged(evt);
}
public void mouseMoved(java.awt.event.MouseEvent evt) {
formMouseMoved(evt);
}
});`
按下按钮的鼠标移动事件将是拖动事件。只要听一听“MouseListener#mouseDragged”,这就是你要找的 按下按钮的鼠标移动事件将是拖动事件。只要听一听“MouseListener#mouseDragged”,这就是你要找的你应该考虑,
- 使用MouseListener和MouseMotionListener的组合,这在MouseAdapter类中很方便地组合李>
- 当鼠标按下时打开绘图
- 发生鼠标滑动时关闭绘图
- 如果绘图处于打开状态,则在鼠标标记内绘图(使用if块)
- 使用
方法和addMouseListener(…)
方法将MouseApter对象添加到组件中两次addMouseMotionListener(…)
你应该考虑,
- 使用MouseListener和MouseMotionListener的组合,这在MouseAdapter类中很方便地组合李>
- 当鼠标按下时打开绘图
- 发生鼠标滑动时关闭绘图
- 如果绘图处于打开状态,则在鼠标标记内绘图(使用if块)
- 使用
方法和addMouseListener(…)
方法将MouseApter对象添加到组件中两次addMouseMotionListener(…)