【Java】Java数字时钟

应用名称:Java数字时钟

用到的知识:Java GUI编程,线程

开发环境:win8+eclipse+jdk1.8

功能说明:可以显示当前系统的年月日、星期以及准确时间,并实时更新显示。

效果图:


源代码:

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JLabel;
import java.awt.Font;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Time extends JFrame implements Runnable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JLabel date;
	private JLabel time;
	public Time() {
		//初始化图形界面
		this.setVisible(true);
		this.setTitle("数字时钟");
		this.setSize(282, 176);
		this.setLocation(200, 200);
		this.setResizable(true);
		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		//时间
		time = new JLabel();
		time.setBounds(31, 54, 196, 59);
		time.setFont(new Font("Arial", Font.PLAIN, 50));
		panel.add(time);
		//日期
		date = new JLabel();
		date.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		date.setBounds(47, 10, 180, 22);
		panel.add(date);
	}
	//用一个线程来更新时间
		 public void run() { 
		 while(true){ 
		 try{ 
			 date.setText(new SimpleDateFormat("yyyy 年 MM 月 dd 日   EEEE").format(new Date()));
			 time.setText(new SimpleDateFormat("HH:mm:ss").format(new Date()));
		 }catch(Throwable t){ 
		  t.printStackTrace(); 
		  } 
		 }
	}
		
	public static void main(String[] args) {
		new Thread(new Time()).start();
	}
}


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值