简体   繁体   English

是否有NO MOTION鼠标监听器?

[英]is there a NO MOTION mouse listener?

I have a map applet, and I have a JLabel following the mouse, whenever the mouse goes over a city the JLable displays the the name of the city and population. 我有一个地图小程序,我有一个跟随鼠标的JLabel,只要鼠标越过一个城市,JLable就会显示城市名称和人口。

I use the mouseMotionListener's MouseMoved method for that, but i want the label to be there only if the mouse stays still for a couple of seconds above the city. 我使用mouseMotionListener的MouseMoved方法,但我希望只有当鼠标在城市上方停留几秒钟时标签才会存在。

I dont know if its because I been working on this code for days now, but i cant seem to think of a solution for this using the MouseMoved method, i tried using timers but that didnt work for me (mayb i just did it wrong cause my brain is burned out) 我不知道是不是因为我现在一直在研究这个代码,但我似乎无法想到使用MouseMoved方法解决这个问题,我尝试使用计时器,但这对我没用(mayb我只是做错了因为我的大脑烧坏了)

so is there a mouse listener for the mouse staying still? 那鼠标是否有老鼠听力? or do you have any recommendations? 或者你有什么建议吗?

here is more or less what I got 这或多或少是我得到的

public void mouseMoved(MouseEvent evt) {
   int x = evt.getX();
   int y = evt.getY();
   boolean aboveCity = false;
   mouseover.setBounds(x+20, y-10, 200, 20); //mouseover is a JLabel

   for (int i=0;i<cityCounter;i++){
      if (city[i].containsPoint(x,y){
         name = city[i].getName();
         population = city[i].getPopulation();
         aboveCity = true;
      }
   }
   if(aboveCity){
      mouseover.setText(name + ", " + population);
   }
   else{
      mouseover.setText("");
   }
}

Use a Java javax.swing.Timer. 使用Java javax.swing.Timer。 Each time the mouse moves, reset the timer. 每次鼠标移动时,重置计时器。 When the timer goes off, the mouse has been "still" for as long as your timer was set for. 当计时器熄灭时,只要您的计时器设置为,鼠标就会“静止”。

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

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