本文實例講述了java實現的動態數字時鐘功能。分享給大家供大家參考,具體如下:
構建:
1. clock繼承 jframe 為運行頁面
2. clocktext 測試類 創建 clock 對象
運行效果:
具體實現:
一、clock類
- 四個jpnal 三個放時間 最后一個放日期
- 放時間的三個jpnal 分別加入 地點 時間 按鈕
- 最后一個按鈕添加日期
具體實現如下:
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
public class clock extends jframe { private jpanel jpanelbeijing; private jpanel jpanelnewyork; private jpanel jpanellondom; private jpanel jpaneldate; private boolean beijingthreadflag_isstart = true ; private boolean newyorkthreadflag_isstart = true ; private boolean londonthreadflag_isstart = true ; public clock() { // todo auto-generated constructor stub jpanelbeijing = new jpanel(); jpanelnewyork = new jpanel(); jpanellondom = new jpanel(); jpaneldate = new jpanel(); inirelations(); inilayout(); jframeclick(); setvisible( true ); setsize( 480 , 225 ); setlocationrelativeto( null ); } private void inilayout() { jpanelbeijing.setlayout( new gridlayout( 3 , 1 )); jpanelnewyork.setlayout( new gridlayout( 3 , 1 )); jpanellondom.setlayout( new gridlayout( 3 , 1 )); } // 關系 private void inirelations() { this .add(borderlayout.west, jpanelbeijing); this .add(borderlayout.center, jpanelnewyork); this .add(borderlayout.east, jpanellondom); this .add(borderlayout.south, jpaneldate); font placefont = new font( "楷體" , font.bold, 36 ); jlabel jlabelbeijing = new jlabel( "北京時間" ); jlabelbeijing.setfont(placefont); jpanelbeijing.add(jlabelbeijing); setwestpanel(); jlabel jlabelnewyork = new jlabel( "紐約時間" ); jlabelnewyork.setfont(placefont); jpanelnewyork.add(jlabelnewyork); setcenterpanel(); jlabel jlabellondon = new jlabel( "倫敦時間" ); jlabellondon.setfont(placefont); jpanellondom.add(jlabellondon); seteastpanel(); setdatepanel(); } private void setwestpanel() { // add time for southpanel jlabel jlabeltime = new jlabel( "加載中." ); jlabeltime.setfont( new font( "宋體" , font.bold, 30 )); timer timeaction = new timer( 1000 , new actionlistener() { public void actionperformed(actionevent e) { long timemillis = system.currenttimemillis(); // 轉換日期顯示格式 simpledateformat time = new simpledateformat( "hh:mm:ss " ); jlabeltime.settext(time.format( new date(timemillis))); } }); timeaction.start(); jpanelbeijing.add(jlabeltime); button button = new button( "北京暫停" ); button.addactionlistener( new actionlistener() { @override public void actionperformed(actionevent e) { // todo auto-generated method stub if (beijingthreadflag_isstart) { timeaction.stop(); button.setlabel( "北京繼續" ); beijingthreadflag_isstart = false ; } else { timeaction.start(); button.setlabel( "北京暫停" ); beijingthreadflag_isstart = true ; } } }); jpanelbeijing.add(button); } private void setcenterpanel() { // add time for southpanel jlabel jlabeltime = new jlabel( "加載中." ); jlabeltime.setfont( new font( "宋體" , font.bold, 30 )); timer timeaction = new timer( 1000 , new actionlistener() { public void actionperformed(actionevent e) { long timemillis = system.currenttimemillis(); // 轉換日期顯示格式 simpledateformat time = new simpledateformat( "hh:mm:ss " ); jlabeltime.settext(time.format( new date(timemillis - 13 * 60 * 60 * 1000 ))); } }); timeaction.start(); jpanelnewyork.add(jlabeltime); button button = new button( "紐約暫停" ); button.addactionlistener( new actionlistener() { @override public void actionperformed(actionevent e) { // todo auto-generated method stub if (newyorkthreadflag_isstart) { timeaction.stop(); button.setlabel( "紐約繼續" ); newyorkthreadflag_isstart = false ; } else { timeaction.start(); button.setlabel( "紐約暫停" ); newyorkthreadflag_isstart = true ; } } }); jpanelnewyork.add(button); } private void seteastpanel() { // add time for southpanel // jlabel jlabeldate = new jlabel("date"); jlabel jlabeltime = new jlabel( "加載中." ); jlabeltime.setfont( new font( "宋體" , font.bold, 30 )); timer timeaction = new timer( 1000 , new actionlistener() { public void actionperformed(actionevent e) { long timemillis = system.currenttimemillis(); // simpledateformat date = new simpledateformat("yyyy 年 mm 月 dd // 日 "); // jlabeldate.settext(" 當前日期: " + date.format(new // date(timemillis))); simpledateformat time = new simpledateformat( "hh:mm:ss " ); jlabeltime.settext(time.format( new time(timemillis - 8 * 60 * 60 * 1000 ))); } }); timeaction.start(); jpanellondom.add(jlabeltime); button button = new button( "倫敦暫停" ); button.addactionlistener( new actionlistener() { @override public void actionperformed(actionevent e) { // todo auto-generated method stub if (londonthreadflag_isstart) { timeaction.stop(); button.setlabel( "倫敦繼續" ); londonthreadflag_isstart = false ; } else { timeaction.start(); button.setlabel( "倫敦暫停" ); londonthreadflag_isstart = true ; } } }); jpanellondom.add(button); // jpanellondom.add(jlabeldate); } private void setdatepanel() { // add time for southpanel jlabel jlabeldate = new jlabel( "加載中." ); timer timeaction = new timer( 1000 , new actionlistener() { public void actionperformed(actionevent e) { long timemillis = system.currenttimemillis(); simpledateformat date = new simpledateformat( "yyyy 年 mm 月 dd 日 " ); jlabeldate.settext( " 當前日期: " + date.format( new date(timemillis))); } }); timeaction.start(); jpaneldate.add(jlabeldate); } private void jframeclick(){ setdefaultcloseoperation(jframe.do_nothing_on_close); //設置不默認關閉 addwindowlistener( new windowlistener() { @override public void windowopened(windowevent e) { // todo auto-generated method stub } @override public void windowiconified(windowevent e) { // todo auto-generated method stub } @override public void windowdeiconified(windowevent e) { // todo auto-generated method stub } @override public void windowdeactivated(windowevent e) { // todo auto-generated method stub } @override public void windowclosing(windowevent e) { // todo auto-generated method stub int x = joptionpane.showconfirmdialog( null , "確認退出么?" , "友情提示" , joptionpane.ok_cancel_option, joptionpane.warning_message); if (x == 0 ) { system.exit( 0 ); } } @override public void windowclosed(windowevent e) { // todo auto-generated method stub } @override public void windowactivated(windowevent e) { // todo auto-generated method stub } }); } } |
二、創建clocktext類用于測試
1
2
3
4
5
|
public class clocktext{ public static void main(string[] args) { new clock(); } } |
希望本文所述對大家java程序設計有所幫助。
原文鏈接:https://blog.csdn.net/qq_43377749/article/details/85096893