本文实例讲述了java实现的计时器【秒表】功能。分享给大家供大家参考,具体如下:
应用名称:java计时器
用到的知识:java gui编程
开发环境:win8+eclipse+jdk1.8
功能说明:计时功能,精确到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;
/**
* 计时器
*/
public class timer extends jframe {
/**
*
*/
private static final long serialversionuid = 1l;
private static final string initial_label_text = "00:00:00 000" ;
// 计数线程
private countingthread thread = new countingthread();
// 记录程序开始时间
private long programstart = system.currenttimemillis();
// 程序一开始就是暂停的
private long pausestart = programstart;
// 程序暂停的总时间
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( "继续" );
}
}
};
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(); // 计数线程一直就运行着
}
// 为窗体面板添加边框
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);
}
// 配置标签
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.zzvips.com 计时器" );
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 );
}
}
}
// 将毫秒数格式化
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:这里再为大家推荐几款时间及日期相关工具供大家参考使用:
unix时间戳(timestamp)转换工具:https://tool.zzvips.com/t/timestamp/
在线秒表计时器:https://tool.zzvips.com/t/miaobiao/
希望本文所述对大家java程序设计有所帮助。
原文链接:https://blog.csdn.net/C_jian/article/details/50506759