在java中创建一个倒计时器(仅限秒和毫秒)

时间:2013-05-29 13:49:16

标签: java swing timer counter

我在java中执行计数计时器。我找到了以下示例:Creating a Count Up timer to Break in java,并且工作正常。但我想用格式“mm:ss:SSS”来显示时间,因为我想测量非常小的时间响应。因此,当它达到1000毫秒时,它是1秒。

我做了一些更改,但我无法以假装格式提供时间。数字开始出现的是秒应该是,而不是毫秒。

如果你有一个更好的例子,那就是我正在关注的那个,那很好。

编辑:这样更好,但它还不能正常工作。计数太慢(这里1分钟对应2分钟)。我的代码在这里:

public class Counter extends JFrame {

    private static final String stop = "Stop";
    private static final String start = "Start";
    private final ClockListener clock = new ClockListener();
    private final Timer timer = new Timer(1, clock);
    private final JTextField tf = new JTextField(9);

public Counter() 
{
    timer.setInitialDelay(0);

    JPanel panel = new JPanel();
    tf.setHorizontalAlignment(JTextField.RIGHT);
    tf.setEditable(false);
    panel.add(tf);
    final JToggleButton b = new JToggleButton(start);
    b.addItemListener(new ItemListener() 
    {
        @Override
        public void itemStateChanged(ItemEvent e) 
        {
            if (b.isSelected()) 
            {
                timer.start();
                b.setText(stop);
            } 
            else 
            {
                timer.stop();
                b.setText(start);
            }
        }
    });
    panel.add(b);

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.add(panel);
    this.setTitle("Timer");
    this.pack();
    this.setLocationRelativeTo(null);
    this.setVisible(true);
}

private class ClockListener implements ActionListener
{
    private int minutes;
    private int seconds;
    private int milliseconds;

    @Override
    public void actionPerformed(ActionEvent e) 
    {
        SimpleDateFormat date = new SimpleDateFormat("mm.ss.SSS");

        if (milliseconds == 1000) 
        {
            milliseconds = 000;
            seconds++;
        }
        if (seconds == 60) {
            seconds = 00;
            minutes++;
        }

        tf.setText(String.valueOf(minutes + ":" + seconds + ":" + milliseconds));
        milliseconds++;
    }
}

public static void main(String[] args) 
{
    EventQueue.invokeLater(new Runnable() 
    {
        @Override
        public void run() {
            Counter clock = new Counter();
            clock.start();
        }
    });
  }
}

1 个答案:

答案 0 :(得分:3)

您正在计算并依赖计时器在1毫秒内到期。计时器(实际上是操作系统)并不能保证在到期之前只发生1毫秒,并且在到期时运行的代码也需要一些时间。相反,使用计时器只是为了触发刷新。我选择了53ms,因为它给用户一种模糊的感觉,毫秒飞过,但注意到用户点击停止后定时器更新1最后一次,不一定是53ms的倍数。显示的时间与计时器到期次数无关,仅与用户按下“开始”时记录的开始时间和当前系统时间无关:

public class Counter extends JFrame {

    private static final String stop = "Stop";
    private static final String start = "Start";
    private final ClockListener clock = new ClockListener();
    private final Timer timer = new Timer(53, clock);
    private final JTextField tf = new JTextField(9);
    private final SimpleDateFormat date = new SimpleDateFormat("mm.ss.SSS");
    private long startTime;

    public Counter() {
        timer.setInitialDelay(0);

        JPanel panel = new JPanel();
        tf.setHorizontalAlignment(JTextField.RIGHT);
        tf.setEditable(false);
        panel.add(tf);
        final JToggleButton b = new JToggleButton(start);
        b.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if (b.isSelected()) {
                    startTime = System.currentTimeMillis();
                    timer.start();
                    b.setText(stop);
                } 
                else {
                    updateClock();
                    startTime = 0;

                    timer.stop();
                    b.setText(start);
                }
            }
        });
        panel.add(b);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.add(panel);
        this.setTitle("Timer");
        this.pack();
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }
    private void updateClock() {
        Date elapsed = new Date(System.currentTimeMillis() - startTime);
        tf.setText(date.format(elapsed));
    }
    private class ClockListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            updateClock();
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                Counter clock = new Counter();
            }
        });
    }
}