【问题标题】:Stopping mouseMoved停止鼠标移动
【发布时间】:2015-07-06 17:29:36
【问题描述】:

我想知道如何阻止 mousedMoved 被解雇。我一直在谷歌搜索,但找不到答案。有办法吗?我正在使用 eclipse 并通过了 mouseevent 方法,但找不到任何东西。

    public class Drawing extends JPanel {

private ArrayList<Point> pointList;
private int counter = 0;

public Drawing() {
    setLayout(new FlowLayout());
    setBackground(Color.white);

    pointList = new ArrayList<Point>();
    addMouseListener(new MouseTrackerListener());


}

public void paintComponent(Graphics pen) {
    super.paintComponent(pen);


    for (int i = 0; i < pointList.size(); i++) {
        Point p = pointList.get(i);
        pen.fillOval(p.x, p.y, 10, 10);
    }

}

private class MouseTrackerListener extends MouseInputAdapter {
    public void mouseClicked(MouseEvent e) {

        counter++;
        if (counter % 2 != 0) {
            addMouseMotionListener(new MouseTrackerListener());


        } else {
            System.out.println("Hi");
        }

    }

    public void mouseMoved(MouseEvent e) {

        Point point = e.getPoint();
        pointList.add(point);

        repaint();

    }
}

【问题讨论】:

  • 别听了?
  • 删除触发更新的MouseMotionListener
  • 给代码添加一个不执行的条件怎么样..所以即使它被触发了,它也不会执行监听器内部的代码?
  • @John3136 你这是什么意思?我是这个部分的新手对不起
  • @MadProgrammer 如何删除它? Idk 如何在它运行后停止它

标签: java mouseevent


【解决方案1】:

您可以创建一个boolean 来切换它是否处于绘图状态。你把boolean命名为isDrawingMode

所以当你点击鼠标的时候..你把它设置为false,如果你再次点击它就会变成true;

您所要做的就是在单击鼠标时切换boolean isDrawingMode

所以你的mousemoved listener 看起来像这样

public void mouseMoved(MouseEvent e) {

        if (!isDrawingMode) return; //if isDrawingMode is false, it will not trigger to draw
        Point point = e.getPoint();
        pointList.add(point);

        repaint();

}

【讨论】:

  • 天哪,非常感谢!一直以来,我一直在搞乱 mouseClicked 但没有碰过 mouseMoved 啊啊再次感谢!
【解决方案2】:

Java

您可以为您的侦听器使用一个公共基类,并在其中有一个静态方法来打开或关闭侦听器:

public abstract class BaseMouseListener implements ActionListener{

    private static boolean active = true;
    public static void setActive(boolean active){
        BaseMouseListener.active = active;
    }

    protected abstract void doPerformAction(ActionEvent e);

    @Override
    public final void actionPerformed(ActionEvent e){
        if(active){
            doPerformAction(e);
        }
    }
}

您的听众必须实现 doPerformAction() 而不是 actionPerformed()。

更多信息: How to temporarily disable event listeners in Swing?

我不知道您使用哪种语言或您的代码是什么。 在 Jquery 中我一般使用以下 2 种方法代码

M1:解除一个事件与另一个事件的绑定。

或 M2:您应该在此事件调用结束时添加 event.stopPropagation() 以停止传播..

M1 代码示例:

else if(e.type == 'click')
{
    $(window).unbind('mousemove')
}
But really you should name the event so you only unbind the appropriate event listener.

Bind : $(window).bind('mousemove.dragging', function(){});

Unbind : $(window).unbind('mousemove.dragging', function(){});

M2 代码示例:

$("#rightSubMenu").mousemove(function(e){
  // You code...
  e.stopPropagation();
});

额外信息

欲了解更多信息,请参阅以下标签 Disable mousemove on click How to stop mousemove() from executing when the element under the mouse moves?

【讨论】:

  • 他使用java。如果你提供一个java代码会更好
  • 它被标记了,但如果你看不到它,我正在使用 Java
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多