日期:2014-05-20 浏览次数:20836 次
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); } }