以下是引用心无旁骛在2010-3-7 00:08:17的发言:
时分秒计时呢
import java.util.*;
import java.awt.*;
import javax.swing.*;
public class Date2010_1 extends JFrame implements Runnable{
private JLabel lbl;
private GregorianCalendar g;
private GregorianCalendar f;
private long dMillis;
private long dDay;
private long dHour;
private long dMin;
private long dSec;
public Date2010_1() {
this.setLayout(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(200, 200, 800, 100);
lbl = new JLabel();
lbl.setBounds(0,0,800,100);
lbl.setFont(new Font(null, Font.BOLD, 30));
lbl.setForeground(Color.red);
this.add(lbl);
this.setTitle("2012");
this.setVisible(true);
}
public void run() {
while(true) {
try {
g = new GregorianCalendar();
f = new GregorianCalendar(2012, Calendar.DECEMBER, 21,0,0,0);
dMillis = f.getTimeInMillis() - g.getTimeInMillis();
dDay = dMillis/(24*60*60*1000);
dHour = 24 - g.get(Calendar.HOUR_OF_DAY);
dMin = 60 - g.get(Calendar.MINUTE) ;
dSec = 60 - g.get(Calendar.SECOND) ;
String str = "今天距离2012年12月21日还有"+dDay+"日" + dHour + "小时" +dMin +"分钟" + dSec + "秒";
lbl.setText(str);
Thread.sleep(1000);
} catch (Exception ex) {
}
}
}
public static void main(String[] args) {
Date2010_1 date2010 = new Date2010_1();
Thread th = new Thread(date2010);
th.start();
}
}