数字时钟

来源:互联网 发布:数据库基础教程 编辑:程序博客网 时间:2024/04/30 05:17
package time;
import java.awt.Toolkit;
import java.util.Calendar;

import javax.swing.*;

public class Time extends JFrame implements Runnable{
    int year = 0;
    int moth = 0;
    int day = 0;
    int hour = 0;
    int minute = 0;
    int second = 0;
int width = Toolkit.getDefaultToolkit().getScreenSize().width;
int height = Toolkit.getDefaultToolkit().getScreenSize().height;
JLabel jl1 = null;

public Time(){
Calendar c=Calendar.getInstance();
year=c.get(Calendar.YEAR);
moth = c.get(Calendar.MONTH)+1;
day=c.get(Calendar.DAY_OF_MONTH);
hour = c.get(Calendar.HOUR);
minute =c.get(Calendar.MINUTE);
second = c.get(Calendar.SECOND);
jl1 = new JLabel(year + "年" + moth + "月" +  day + "日"  + hour+ "时" + minute + "分" +  second + "秒");
jl1.setBounds(200, 100, 100, 50);
this.add(jl1);

this.setTitle("时间线程");
this.setLocation((width - 500) / 2, (height - 500) / 2);
this.setSize(200, 200);
this.setVisible(true);
}

public static void main(String[] args) {
Runnable aRunnable = new Time();
new Thread(aRunnable).start();

}
@Override
public void run() {
while(true){
try {  
Calendar c=Calendar.getInstance();
year=c.get(Calendar.YEAR);
moth = c.get(Calendar.MONTH)+1;
day=c.get(Calendar.DAY_OF_MONTH);
hour = c.get(Calendar.HOUR);
minute =c.get(Calendar.MINUTE);
second = c.get(Calendar.SECOND);
 
 jl1.setText(year + "年" + moth + "月" +  day + "日"  + hour+ "时" + minute + "分" +  second + "秒");
 Thread.sleep(100);
} catch (InterruptedException e) {
 e.printStackTrace();
}
}

}
}
0 0
原创粉丝点击