简体   繁体   English

仅当鼠标悬停在元素上至少一段特定时间时,如何触发MouseMove事件?

[英]How to have a MouseMove event fire only if the mouse is hovered over an element for at least a specific amount of time?

I want to display a JFrame when a user move the mouse pointer inside over a certain area of a JPanel with a little delay. 当用户将鼠标指针移动到JPanel的某个区域上并稍微延迟时,我想显示一个JFrame I show the JFrame by attaching a MouseAdapter to the JPanel and overriding the MouseMove method. 我通过将MouseAdapter附加到JPanel并覆盖MouseMove方法来显示JFrame

addMouseListener(new MouseAdapter() {
  @Override
  public void mouseMoved(MouseEvent e) {
    Point p= e.getLocationOnScreen();
    //check if Point p is within the boundaries of a rectangle.          
    //show a JFrame
  }                
});

To obtain the delay I think that I should use something like a Thread by using sleep , with the condition that it must be interrupted if the mouse is moved out of the boundaries, but I'm not sure that this is the best approach. 为了获得延迟,我认为我应该使用类似Thread的东西,使用sleep ,条件是如果鼠标移出边界必须中断,但我不确定这是最好的方法。 Currently, I only see on SO questions related to JavaScript. 目前,我只看到与JavaScript相关的SO问题。 What's the best way to do it in Java? 在Java中最好的方法是什么?

You can use the mouseEntered and mouseExited events from the MouseAdapter class. 您可以使用MouseAdapter类中的mouseEnteredmouseExited事件。

You can set a timer on the mouseEntered method and check if the time spent on the object is >= the designated time in the mouseExited method and if so perform the action. 您可以在mouseEntered方法上设置计时器,并检查在对象上花费的时间是否>= mouseExited方法中的指定时间,如果是,则执行操作。

For the scenario of mouse left at the same point, you can use the Timer with a delay of how many ever seconds you want and setup a handler at mouseExited to stop the timer if the pointer is exited before the designated time. 对于鼠标停留在同一点的情况,您可以使用Timer延迟所需的秒数,并在mouseExited处设置处理程序,以便在指定时间之前退出指针时停止计时器。

Maybe a good solution is by using java.util.Timer . 也许一个好的解决方案是使用java.util.Timer

addMouseListener(new MouseAdapter() {
 private int delay = 1000;//1 second
 private Timer timer = null;
 private int startTime =0;

  @Override
  public void mouseMoved(MouseEvent e) {
    Point p= e.getLocationOnScreen();
    boolean pointInArea=false;
    //check if Point p is within the boundaries of a rectangle.          
    if(pointInArea){
        //A JFrame is queued to be shown
        if(System.currentTimeMillis()-startTime>delay){
          //A JFrame has been already shown, then show a new one
          startTime = System.currentTimeMillis();
          timer = new Timer();                    
          timer.schedule(new TimerTask(){
              @Override
              public void run() {
                //LAUNCH JFrame
              }

          }, delay);                     
        }
     }
     else (!pointInArea && timer != null){
        timer.cancel();
        timer = null;                        
     }
  }                
});

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

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