首页 > 编程 > Java > 正文

Java实现的简单数字时钟功能示例

2019-11-26 09:15:37
字体:
来源:转载
供稿:网友

本文实例讲述了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();  }}

PS:这里再为大家推荐几款时间及日期相关工具供大家参考使用:

Unix时间戳(timestamp)转换工具:
http://tools.VeVB.COm/code/unixtime

在线日期/天数计算器:
http://tools.VeVB.COm/jisuanqi/date_jisuanqi

在线日期计算器/相差天数计算器:
http://tools.VeVB.COm/jisuanqi/datecalc

在线日期天数差计算器:
http://tools.VeVB.COm/jisuanqi/onlinedatejsq

更多关于java相关内容感兴趣的读者可查看本站专题:《java日期与时间操作技巧汇总》、《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》和《Java缓存操作技巧汇总

希望本文所述对大家java程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表