0
public class Blank extends WindowController
{
    private int mouseClicks;

    public void onMousePress(Location point)
    {
         mouseClicks++;
    }
}

我的目标是如果 mouseClicks 每秒增加一次,而只需单击一次即可启动它。

4

3 回答 3

1

这是我能得到的最佳解决方案。

public class Blank extends WindowController
{
    private final AtomicInteger mouseClicks = new AtomicInteger();
    private boolean hasStarted = false;

    public void onMousePress(Location point)
    {
       if(!hasStarted){
         hasStarted = true;
         Thread t = new Thread(){
             public void run(){
                 while(true){
                     mouseClicks.incrementAndGet(); //adds one to integer
                     Thread.sleep(1000); //surround with try and catch
                 }
             }
         };
         t.start();
      }
    }
}
于 2014-11-03T02:33:24.160 回答
0

研究使用Thread.sleep(1000);暂停执行一秒钟。

http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html

于 2014-11-03T01:46:35.373 回答
-1

您可以测试自上次单击以来经过的时间。

long lLastClickTime = Long.MIN_VALUE;

public void onMousePress(Location point) {
   final long lCurrentTime = System.currentTimeMillis();
   if(lCurrentTime - lClickLastTime >= 1000) {
      mouseClicks++;
      lLastClickTime = lCurrentTime;
   }         
}
于 2014-11-03T02:09:41.100 回答