【问题标题】:Reset timer in JavaJava中的重置计时器
【发布时间】:2010-10-14 20:31:18
【问题描述】:

单击按钮后,我尝试根据当前时间重置计时器,但不起作用。

private long startTime  = System.currentTimeMillis();
Timer timer  = new Timer(1000, this);
timer.start();

timer.stop();
long endTime    = System.currentTimeMillis();
long timeInMilliseconds = (endTime - startTime);

timer.reset();

【问题讨论】:

  • 你期望 Timer.reset 做什么,它在做什么?
  • 你需要详细说明你的问题并注释代码sn-p:当它的各个部分运行时等等。
  • 实际上我需要根据最新的系统计时器重置时间..但我现在得到了解决方案。我只需要将 startTime = System.currentTimeMillis();再次在按钮 actionListener 方法中。
  • JDK 中的 Timer 类都没有 reset() 方法,因此我们需要更多信息来了解您要执行的操作。
  • 如果您有解决方案,请将其添加到此处的答案中并将其标记为已接受。

标签: java timer reset


【解决方案1】:

我的魔法水晶球说你使用的是 javax.swing.Timer 并且没有 reset() 方法,它被称为 restart()。

但那可能是错误的,如果你能更明确地说明你在做什么,那就太好了......

【讨论】:

  • 其实我需要的是重新启动计时器到当前系统时间。但它现在解决了......我只需要将 startTime = System.currentTimeMillis() 再次放在 ActionListener 而不是主类中。
【解决方案2】:

我的程序的解决方案。谢谢大家。

   public class mainClass {
        private long startTime  = System.currentTimeMillis();
        Timer timer  = new Timer(1000, this);
        .....
    }

    public mainClass {
        timer.start();
    }

    //Everytime the button stop clicked, the time will stop and reset to the most current time of the system
    public actionPerformed () {
        timer.stop();
        long endTime    = System.currentTimeMillis();
        long timeInMilliseconds = (endTime - startTime);

        **startTime  = System.currentTimeMillis();** ACCEPTED
    }

【讨论】:

  • 为了将来参考,所有 Java 类(在你的例子中是“mainClass”)都应该以大写字母开头,你可能不应该在类名中使用“类”这个词。
猜你喜欢
  • 2015-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-06
  • 1970-01-01
  • 2015-11-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多