本文實例講述了java實現的簡單數字時鐘功能。分享給大家供大家參考,具體如下:
應用名稱:java數字時鐘
用到的知識:java gui編程,線程
開發環境:win8+eclipse+jdk1.8
功能說明:可以顯示當前系統的年月日、星期以及準確時間,并實時更新顯示。
效果圖:
源代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
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)轉換工具:https://tool.zzvips.com/t/timestamp/
在線秒表計時器:https://tool.zzvips.com/t/miaobiao/
希望本文所述對大家java程序設計有所幫助。
原文鏈接:https://blog.csdn.net/C_jian/article/details/50505130