日期:2014-05-20  浏览次数:21183 次

怎么用Java实现精确到毫秒的倒计时器
现在要用Java实现一个倒计时器,我采用的办法是:先获得总的毫秒数matchTime,然后用Timer,每隔一毫秒就让matchTime减1,然后再把matchTime转换成时间的格式。但是这样做出来的时间走的比现实时间要慢得多,如果改成每100秒减100次,就和现实时间差不多了,这也就是说精确到了0.1秒。但是有没有办法精确到毫秒呢?

------解决方案--------------------
看起来是timer的精度不够
------解决方案--------------------
getMilliseconds(),返回毫秒数。
------解决方案--------------------
System.nanoTime()
------解决方案--------------------
windows系统的计时精确度在15毫秒左右。
------解决方案--------------------
Java code
import java.util.Date;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.awt.Font;
import java.awt.BorderLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

/**
 * TimerFrame - 倒计时窗口
 * @author SageZk
 * @version 1.0
 */
@SuppressWarnings("serial")
public class TimerFrame extends JFrame {

    private long time = (long) (1.2 * 60 * 1000L);  //倒计时时间(单位毫秒)
    private JLabel lblTime;
    private Thread runner;

    public TimerFrame() {
        super("TimerFrame");
        this.lblTime = new JLabel("单击开始");
        this.lblTime.setFont(new Font("Monospaced", Font.BOLD, 60));
        this.lblTime.setHorizontalAlignment(SwingConstants.CENTER);
        this.lblTime.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (runner != null) runner.start();
            }
        });
        this.runner = new Thread(new Runnable() {
            public void run() {
                Date t = new Date();
                StringWriter sw = new StringWriter();
                StringBuffer sb = sw.getBuffer();
                PrintWriter pw = new PrintWriter(sw);
                long cur = 0L, end = System.currentTimeMillis() + time;
                while ((cur = end - System.currentTimeMillis()) > 0) {
                    t.setTime(cur);
                    pw.format("%1$tM:%1$tS.%tL", t);
                    pw.flush();
                    lblTime.setText(sb.toString());
                    sb.setLength(0);
                    try {
                        Thread.sleep(6L);
                    } catch (InterruptedException e) {
                    }
                }
                lblTime.setText("00:00.000");
                try {
                    Thread.sleep(1200L);
                } catch (InterruptedException e) {
                }
                lblTime.setText("Bomb!!!");
            }
        });
        this.runner.setDaemon(true);
        getContentPane().add(this.lblTime, BorderLayout.CENTER);
        setResizable(false);
        setBounds(0, 0, 460, 330);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TimerFrame().setVisible(true);
    }

}

------解决方案--------------------
顶楼上的.
------解决方案--------------------
import java.util.Date;
import java.io.PrintWriter;
import java.awt.Font;
import java.swing.JFrame;
 public TimerFrame() {
super("TimerFrame");
this.lblTime = new JLabel("单击开始");
this.lblTime.setFont(new Font("Monospaced", Font.BOLD, 60));
this.lblTime.setHorizontalAlignment(SwingConstants.CENTER);
this.lblTime.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (runner != null) runner.start();