简体   繁体   English

鼠标侦听器,释放鼠标时停止执行块

[英]Mouse-listener that stops a block from executing when the mouse is released

In Java, I need to integrate a mouse Listener that moves a motor. 在Java中,我需要集成一个用于移动电动机的鼠标侦听器。 However here is the catch, there is no stop function. 但是,这里有问题,没有停止功能。 Thereby, the mousepressed should move the motor, while the mouseReleased should stop it, without actually sending a stop command but rather by stopping the mouse pressed block from executing. 因此,被按下的鼠标应移动电动机,而被释放的鼠标应将其停止,而无需实际发送停止命令,而是要停止执行被按下的鼠标块。 Any ideas on how this can be implemented? 关于如何实施的任何想法? And is it possible to repeat the command under mousePressed as long as the mouse is pressed? 只要按下鼠标,是否可以在mousePressed下重复命令? The following code is to illustrate my current program, which uses a stop function, which i would rather avoid. 以下代码说明了我当前的程序,该程序使用了stop函数,我宁愿避免这样做。

    n2Button.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            if ((e.getModifiers() & InputEvent.BUTTON1_MASK) != 0) {
                try {
                    //                        moveUp();
                    session.motion(0, 0, -500);
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        }

        public void mouseReleased(MouseEvent e) {
            if ((e.getModifiers() & InputEvent.BUTTON1_MASK) != 0) {
                try {
                    session.stop();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        }
    });

You rather want to have mouse motion listener [Example][1] [1]: https://docs.oracle.com/javase/tutorial/uiswing/events/mousemotionlistener.html And i also do not see the problem to have session.stop(); 您宁愿拥有鼠标移动侦听器[示例] [1] [1]: https : //docs.oracle.com/javase/tutorial/uiswing/events/mousemotionlistener.html而且我也看不到有会话的问题。停(); only the movement function i guess must be executed in another thread 我猜只有运动功能必须在另一个线程中执行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM