java倒计时精确到天,如何用Java实现精确到毫秒的倒计时器

Java codeimport 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();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值