本文實(shí)例講述了java實(shí)現(xiàn)的計(jì)時(shí)器【秒表】功能。分享給大家供大家參考,具體如下:
應(yīng)用名稱:java計(jì)時(shí)器
用到的知識(shí):java gui編程
開發(fā)環(huán)境:win8+eclipse+jdk1.8
功能說明:計(jì)時(shí)功能,精確到1毫秒,可暫停。
效果圖:
源代碼:
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
import javax.swing.*; import java.awt.headlessexception; import java.awt.borderlayout; import java.awt.flowlayout; import java.awt.font; import java.awt.event.actionlistener; import java.awt.event.actionevent; /** * 計(jì)時(shí)器 */ public class timer extends jframe { /** * */ private static final long serialversionuid = 1l; private static final string initial_label_text = "00:00:00 000" ; // 計(jì)數(shù)線程 private countingthread thread = new countingthread(); // 記錄程序開始時(shí)間 private long programstart = system.currenttimemillis(); // 程序一開始就是暫停的 private long pausestart = programstart; // 程序暫停的總時(shí)間 private long pausecount = 0 ; private jlabel label = new jlabel(initial_label_text); private jbutton startpausebutton = new jbutton( "開始" ); private jbutton resetbutton = new jbutton( "清零" ); private actionlistener startpausebuttonlistener = new actionlistener() { public void actionperformed(actionevent e) { if (thread.stopped) { pausecount += (system.currenttimemillis() - pausestart); thread.stopped = false ; startpausebutton.settext( "暫停" ); } else { pausestart = system.currenttimemillis(); thread.stopped = true ; startpausebutton.settext( "繼續(xù)" ); } } }; private actionlistener resetbuttonlistener = new actionlistener() { public void actionperformed(actionevent e) { pausestart = programstart; pausecount = 0 ; thread.stopped = true ; label.settext(initial_label_text); startpausebutton.settext( "開始" ); } }; public timer(string title) throws headlessexception { super (title); setdefaultcloseoperation(exit_on_close); setlocation( 300 , 300 ); setresizable( false ); setupborder(); setuplabel(); setupbuttonspanel(); startpausebutton.addactionlistener(startpausebuttonlistener); resetbutton.addactionlistener(resetbuttonlistener); thread.start(); // 計(jì)數(shù)線程一直就運(yùn)行著 } // 為窗體面板添加邊框 private void setupborder() { jpanel contentpane = new jpanel( new borderlayout()); contentpane.setborder(borderfactory.createemptyborder( 5 , 5 , 5 , 5 )); this .setcontentpane(contentpane); } // 配置按鈕 private void setupbuttonspanel() { jpanel panel = new jpanel( new flowlayout()); panel.add(startpausebutton); panel.add(resetbutton); add(panel, borderlayout.south); } // 配置標(biāo)簽 private void setuplabel() { label.sethorizontalalignment(swingconstants.center); label.setfont( new font(label.getfont().getname(), label.getfont().getstyle(), 40 )); this .add(label, borderlayout.center); } // 程序入口 public static void main(string[] args) { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (exception e) { e.printstacktrace(); } timer frame = new timer( "www.ythuaji.com.cn 計(jì)時(shí)器" ); frame.pack(); frame.setvisible( true ); } private class countingthread extends thread { public boolean stopped = true ; private countingthread() { setdaemon( true ); } @override public void run() { while ( true ) { if (!stopped) { long elapsed = system.currenttimemillis() - programstart - pausecount; label.settext(format(elapsed)); } try { sleep( 1 ); // 1毫秒更新一次顯示 } catch (interruptedexception e) { e.printstacktrace(); system.exit( 1 ); } } } // 將毫秒數(shù)格式化 private string format( long elapsed) { int hour, minute, second, milli; milli = ( int ) (elapsed % 1000 ); elapsed = elapsed / 1000 ; second = ( int ) (elapsed % 60 ); elapsed = elapsed / 60 ; minute = ( int ) (elapsed % 60 ); elapsed = elapsed / 60 ; hour = ( int ) (elapsed % 60 ); return string.format( "%02d:%02d:%02d %03d" , hour, minute, second, milli); } } } |
ps:這里再為大家推薦幾款時(shí)間及日期相關(guān)工具供大家參考使用:
unix時(shí)間戳(timestamp)轉(zhuǎn)換工具:https://tool.zzvips.com/t/timestamp/
在線秒表計(jì)時(shí)器:https://tool.zzvips.com/t/miaobiao/
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
原文鏈接:https://blog.csdn.net/C_jian/article/details/50506759