本文实例为大家分享了java实现动态时钟设置闹钟功能,供大家参考,具体内容如下
显示如上图所示的动态时钟,并且可以设置闹钟,播放mp3。
首先用到的是时钟(timer)和日历(calendar)得到系统的当前时间。
代码如下:
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( "动态时钟" ); clockpanel = new mypanel(); add(clockpanel); timer t = new timer(); task task = new task(); t.schedule(task, 0 , 1000 ); //每秒刷新一次 } file file = new file( "当我想你的时候.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( "当我想你的时候.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);//窗体显示在屏幕中央 //输入要设置的闹钟时间 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 = "当前时间:" + 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文件需要下载对应的jar包,否则不能播放。
下载地址:java实现动态时钟
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://blog.csdn.net/qq_32353771/article/details/51726312