我在添加到另一个类(JFrame)的JPanel类中有以下代码。我想要实现的是某种秒表程序。停止秒表
startBtn.addActionListener(new startListener());
class startListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Timer time = new Timer();
time.scheduleAtFixedRate(new Stopwatch(), 1000, 1000);
}
}
这是另一类,它基本上是任务。
public class Stopwatch extends TimerTask {
private final double start = System.currentTimeMillis();
public void run() {
double curr = System.currentTimeMillis();
System.out.println((curr - start)/1000);
}
}
计时器工作正常,这绝对不是完整的,但我不知道如何编码应停止计时器的停止按钮。对此有何建议? BTW我使用java.util.Timer中
编辑:我希望能够在停止之后再次启动它(无计时器被重置)
对不起,这不正是我一直在寻找。我编辑了我的问题。 – 2010-04-05 00:40:56