时钟(图形界面+多线程)

package CrescentMoonPackage;

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.util.Calendar;

import javax.swing.JFrame;
import javax.swing.JLabel;

public class ShowClock extends JFrame {

	JLabel clock=new JLabel();
	Container c;
	ShowCurrentTime st;
	public ShowClock(){
		c=this.getContentPane();
		c.setLayout(new FlowLayout());
		c.add(clock);
		clock.setFont(new Font("黑体",Font.BOLD,20));
		clock.setForeground(new Color(225,0,0));
		st=new ShowCurrentTime(clock);
		st.start();
		this.setSize(400,300);
		this.setVisible(true);
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ShowClock sc=new ShowClock();
	}
}
class ShowCurrentTime extends Thread{
	JLabel clock;
	public ShowCurrentTime(JLabel clock){
		this.clock=clock;
	}
	public void run(){
		while(true){
		Calendar c=Calendar.getInstance();
		int year=c.get(Calendar.YEAR);
		int month=c.get(Calendar.MONTH);
		int day=c.get(Calendar.DATE);
		int hour=c.get(Calendar.HOUR);
		int minutes=c.get(Calendar.MINUTE);
		int second=c.get(Calendar.SECOND);
		String ss=year+"年"+month+"月"+day+"号   "+hour+"时"+minutes+"分"+second+"秒";
		clock.setText(ss);
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		}
	}
}

时钟(图形界面+多线程)_第1张图片

你可能感兴趣的:(java)