1

我的程序中的鼠标事件有问题。我正在尝试使用画布编写绘图程序。

如果他左键单击并移动鼠标,用户应该绘制。所以我定义了一个Drawer包含 a的类boolean allow_draw,并添加了一个方法draw

drawmousemoved使用画布中的事件调用,并使用andallow_draw设置 true 和 false 。mousepressedreleased

但是,mousemoved当我按下鼠标按钮时不会触发...

我的问题是:按下鼠标按钮时如何听鼠标移动。

希望你知道我在找什么:)

4

3 回答 3

2

你能发布你的源代码吗?请尝试添加 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);
        }
    });`
于 2012-11-02T23:55:25.510 回答
1

你应该考虑,

  • 使用 MouseListener 和 MouseMotionListener 的组合,在 MouseAdapter 类中方便地组合。
  • 当 mousePressed 发生时打开绘图。
  • 发生 mouseReleased 时关闭绘图
  • 如果绘图处于打开状态(使用 if 块),则在 mouseDragged 内绘图。
  • addMouseListener(...)使用方法和方法将 MouseAdapter 对象两次添加到组件中addMouseMotionListener(...)
于 2012-11-03T00:17:11.247 回答
0

按下按钮的鼠标移动事件将是拖动事件。只需听“MouseListener#mouseDragged”,这就是您要找的。

于 2012-11-02T23:55:48.683 回答