停止秒表 [英] Stop a stopwatch

查看:169
本文介绍了停止秒表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JPanel类中有以下代码,该类被添加到另一个类(JFrame)中。我想要实现的是某种秒表程序。

I have the following code in a JPanel class which is added to a another class (JFrame). What I'm trying to implement is some sort of a stopwatch program.

startBtn.addActionListener(new startListener());

class startListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        Timer time = new Timer();
        time.scheduleAtFixedRate(new Stopwatch(), 1000, 1000);
    }
}

这是另一个基本上是任务的类。

This is another class which basically the task.

public class Stopwatch extends TimerTask {

    private final double start = System.currentTimeMillis();

    public void run() {
        double curr = System.currentTimeMillis();
        System.out.println((curr - start) / 1000);
    }
}

计时器工作正常,这绝对不是完整的但我不知道如何编写应该停止计时器的停止按钮。有什么建议吗?顺便说一句我正在使用java.util.timer

The timer works fine and this is definitely far from complete but I'm not sure how to code the stop button which should stop the timer. Any advice on this? BTW I'm using java.util.timer

编辑:我希望能够在停止后再次启动它(没有重置计时器)

I want to be able to start it again after stopping it (without the timer being reset)

推荐答案

如果 javax.swing.Timer 是可接受的替代方案,正如@Ash建议的,这是一个例子。

If javax.swing.Timer is an acceptable alternative, as @Ash suggests, here is an example.

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;

/** @see http://stackoverflow.com/questions/2576909 */
public class JTimeLabel extends JLabel implements ActionListener {

    private static final String Start = "Start";
    private static final String Stop = "Stop";
    private DecimalFormat df = new DecimalFormat("000.0");
    private Timer timer = new javax.swing.Timer(100, this);
    private long now = System.currentTimeMillis();

    public JTimeLabel() {
        this.setHorizontalAlignment(JLabel.CENTER);
        this.setText(when());
    }

    public void actionPerformed(ActionEvent ae) {
        setText(when());
    }

    public void start() {
        timer.start();
    }

    public void stop() {
        timer.stop();
    }

    private String when() {
        return df.format((System.currentTimeMillis() - now) / 1000d);
    }

    private static void create() {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final JTimeLabel jtl = new JTimeLabel();
        jtl.setFont(new Font("Dialog", Font.BOLD, 32));
        f.add(jtl, BorderLayout.CENTER);

        final JButton button = new JButton(Stop);
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String cmd = e.getActionCommand();
                if (Stop.equals(cmd)) {
                    jtl.stop();
                    button.setText(Start);
                } else {
                    jtl.start();
                    button.setText(Stop);
                }

            }
        });
        f.add(button, BorderLayout.SOUTH);
        f.pack();
        f.setVisible(true);
        jtl.start();
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                create();
            }
        });
    }
}

这篇关于停止秒表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆