创建一个Count Up计时器以在Java中断

时间:2011-04-21 14:49:18

标签: java swing timer jpanel jlabel

我正在尝试为我正在编写的拼图应用程序实现基于计时器的评分系统。

有人可以向我提供一个示例案例,即在摇摆中创建一个JLabel或Panel,其中包含一个可见计数的计时器(从0开始的秒数),在一个方法的调用中停止。并返回其值。

示例:

小时:分钟:秒  [00:00:00]  [00:00:01] ..等。覆盖以前的条目。

由于

编辑:这是由trashgod链接的示例代码的改编: ClockExample, 它使用简单的if语句来显示小时分钟和秒......

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.Timer;

/** @see https://stackoverflow.com/questions/5528939*/
class ClockExample extends JFrame {

private static final int N = 60;
private static final String stop = "Stop";
private static final String start = "Start";
private final ClockListener cl = new ClockListener();
private final Timer t = new Timer(1000, cl);
private final JTextField tf = new JTextField(8);

public ClockExample() {
    t.setInitialDelay(0);

    JPanel panel = new JPanel();
    tf.setHorizontalAlignment(JTextField.RIGHT);
    tf.setEditable(false);
    panel.add(tf);
    final JToggleButton b = new JToggleButton(stop);
    b.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(ItemEvent e) {
            if (b.isSelected()) {
                t.stop();
                b.setText(start);
            } else {
                t.start();
                b.setText(stop);
            }
        }
    });
    panel.add(b);

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

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

private class ClockListener implements ActionListener {

    private int hours;
    private int minutes;
    private int seconds;
    private String hour;
    private String minute;
    private String second;

    @Override
    public void actionPerformed(ActionEvent e) {
        NumberFormat formatter = new DecimalFormat("00");
        if (seconds == N) {
            seconds = 00;
            minutes++;
        }

        if (minutes == N) {
            minutes = 00;
            hours++;
        }
        hour = formatter.format(hours);
        minute = formatter.format(minutes);
        second = formatter.format(seconds);
        tf.setText(String.valueOf(hour + ":" + minute + ":" + second));
        seconds++;
    }
}

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

        @Override
        public void run() {
            ClockExample clock = new ClockExample();
            clock.start();
        }
    });
  }
}

再次感谢所有人!

1 个答案:

答案 0 :(得分:4)

有点过于复杂了。您可以使用java.util.Date和java.text.SimpleDateFormat来显示传递的时间,而不是使用秒,分钟,小时。

start = new Date(); // time right now
sdf = new SimpleDateFormat("hh:mm:ss");

然后稍后显示已经过了几个小时:

Date now = new Date();
sdf.format(new Date(now.getTime() - start.getTime()); //create a date based on time passed

这将具有相同的效果,更清晰,更明确。