【Java】Java数字时钟

来源:互联网 发布:装了linux系统无法打开 编辑:程序博客网 时间:2024/06/05 11:09

应用名称: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();}}


0 0
原创粉丝点击