本文實例為大家分享了java實現(xiàn)動態(tài)時鐘設(shè)置鬧鐘功能,供大家參考,具體內(nèi)容如下
顯示如上圖所示的動態(tài)時鐘,并且可以設(shè)置鬧鐘,播放mp3。
首先用到的是時鐘(timer)和日歷(calendar)得到系統(tǒng)的當(dāng)前時間。
代碼如下:
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
|
import java.awt.graphics; import java.awt.graphics2d; import java.awt.geom.ellipse2d; import java.awt.geom.line2d; import java.io.bufferedinputstream; import java.io.file; import java.io.fileinputstream; import java.io.ioexception; import java.util.calendar; import java.util.gregoriancalendar; import java.util.timer; import java.util.timertask; import javax.media.cannotrealizeexception; import javax.media.manager; import javax.media.medialocator; import javax.media.noplayerexception; import javax.swing.jframe; import javax.swing.joptionpane; import javax.swing.jpanel; import javazoom.jl.player.player; public class clock extends jframe { mypanel clockpanel; ellipse2d. double e; int x; int y; line2d. double hourline; line2d. double minline; line2d. double secondline; gregoriancalendar calendar; int hour; int minute; int second; string timestr = "" ; static int sethour; static int setminute; static int setsecond; public static final int x = 60 ; public static final int y = 60 ; public static final int x_begin = 10 ; public static final int y_begin = 10 ; public static final int radian = 50 ; public clock(){ setsize( 300 , 200 ); settitle( "動態(tài)時鐘" ); clockpanel = new mypanel(); add(clockpanel); timer t = new timer(); task task = new task(); t.schedule(task, 0 , 1000 ); //每秒刷新一次 } file file = new file( "當(dāng)我想你的時候.mp3" ); public static void playmusic(file file) { //顯示mp3文件的絕對路徑 try { javax.media.player player = null ; if (file.exists()) { medialocator locator = new medialocator( "file:" + file.getabsolutepath()); system.out.println(file.getabsolutepath()); player = manager.createrealizedplayer(locator); player.prefetch(); // ?準備讀取 player.start(); // 開始讀取 } else { system.out.println( "沒找到文件" ); } } catch (cannotrealizeexception ex) { ex.printstacktrace(); } catch (noplayerexception ex) { ex.printstacktrace(); } catch (ioexception ex) { ex.printstacktrace(); } } public void play() { //播放mp3文件 try { bufferedinputstream buffer = new bufferedinputstream( new fileinputstream( "當(dāng)我想你的時候.mp3" )); player player = new player(buffer); player.play(); } catch (exception e) { system.out.println(e); } } public static void main(string[] args) { clock t = new clock(); t.setdefaultcloseoperation(jframe.exit_on_close); t.setvisible( true ); //t.setlocationrelativeto(null);//窗體顯示在屏幕中央 //輸入要設(shè)置的鬧鐘時間 sethour = integer.parseint(joptionpane.showinputdialog( "請輸入小時:" )); setminute = integer.parseint(joptionpane.showinputdialog( "請輸入分鐘:" )); setsecond = integer.parseint(joptionpane.showinputdialog( "請輸入秒:" )); } class mypanel extends jpanel { public mypanel() { e = new ellipse2d. double (x_begin, y_begin, 100 , 100 ); hourline = new line2d. double (x, y, x, y); minline = new line2d. double (x, y, x, y); secondline = new line2d. double (x, y, x, y); } public void paintcomponent(graphics g) { super .paintcomponent(g); graphics2d g2 = (graphics2d) g; g2.drawstring( "12" , 55 , 25 ); //整點時間 g2.drawstring( "6" , 55 , 105 ); g2.drawstring( "9" , 15 , 65 ); g2.drawstring( "3" , 100 , 65 ); g2.drawstring(timestr, 0 , 130 ); g2.draw(e); g2.draw(hourline); //時針 g2.draw(minline); //分針 g2.draw(secondline); //秒針 } } class task extends timertask { public void run() { calendar = new gregoriancalendar(); hour = calendar.get(calendar.hour); minute = calendar.get(calendar.minute); second = calendar.get(calendar.second); if (sethour == hour && setminute == minute && setsecond == second){ playmusic(file); play(); } timestr = "當(dāng)前時間:" + hour + " : " + minute + " : " + second; hourline.x2 = x + 40 * math.cos(hour * (math.pi / 6 ) - math.pi / 2 ); hourline.y2 = y + 40 * math.sin(hour * (math.pi / 6 ) - math.pi / 2 ); minline.x2 = x + 45 * math.cos(minute * (math.pi / 30 ) - math.pi / 2 ); minline.y2 = y + 45 * math.sin(minute * (math.pi / 30 ) - math.pi / 2 ); secondline.x2 = x + 50 * math.cos(second * (math.pi / 30 ) - math.pi / 2 ); secondline.y2 = y + 50 * math.sin(second * (math.pi / 30 ) - math.pi / 2 ); repaint(); } } } |
其中播放mp3文件需要下載對應(yīng)的jar包,否則不能播放。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.csdn.net/qq_32353771/article/details/51726312