實現(xiàn)效果圖:
代碼如下
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
package cn.hncu.games; import java.applet.applet; import java.applet.audioclip; import java.awt.color; import java.awt.font; import java.awt.event.mouseadapter; import java.awt.event.mouseevent; import java.awt.event.mouselistener; import java.io.file; import java.net.url; import javax.swing.defaultlistmodel; import javax.swing.imageicon; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jlist; import javax.swing.listmodel; import javax.swing.event.listselectionevent; import javax.swing.event.listselectionlistener; public class musicplayer extends jframe{ //顯示(歌曲名稱)播放狀態(tài)的標簽 jlabel songnamelabel = null ; //四個播放功能鍵按鈕 jbutton btnlast = null ; //上一曲 jbutton btnplay = null ; //播放/停止 jbutton btnnext = null ; //下一曲 jbutton btnloop = null ; //循環(huán) //歌曲列表 jlist songslist = null ; audioclip songs[] = null ; audioclip currentsong = null ; int index= 0 ; //當前歌曲在jlist中的位置(序號) //歌曲文件名數(shù)組---string string[] strsongnames={ "song1.wav" , "song2.wav" , "song3.wav" , "song4.wav" , "song5.wav" , "song6.wav" }; final string dir= "songs\\" ; //播放音樂的線程 thread playerthread= null ; boolean isplayorstop = true ; //true代表播放狀態(tài) boolean isloop = false ; //是否為循環(huán)狀態(tài) public musicplayer() { super ( "音樂播放器" ); setbounds( 300 , 50 , 310 , 500 ); setdefaultcloseoperation(exit_on_close); setlayout( null ); //hello(); //顯示(歌曲名稱)播放狀態(tài)的標簽 songnamelabel = new jlabel(); font songnamefont = new font( "黑體" ,font.italic, 18 ); songnamelabel.setfont(songnamefont); songnamelabel.settext( "我的音樂播放器" ); songnamelabel.setbounds( 10 , 10 , 300 , 40 ); getcontentpane().add(songnamelabel); //四個播放功能鍵按鈕 btnlast = new jbutton(); btnplay = new jbutton(); btnnext = new jbutton(); btnloop = new jbutton(); //位置大小 btnlast.setbounds( 10 , 70 , 50 , 40 ); btnplay.setbounds( 70 , 70 , 50 , 40 ); btnnext.setbounds( 130 , 70 , 50 , 40 ); btnloop.setbounds( 190 , 70 , 50 , 40 ); //設(shè)置圖片 btnlast.seticon( new imageicon( "images2/1.png" )); btnplay.seticon( new imageicon( "images2/2.png" )); btnnext.seticon( new imageicon( "images2/3.png" )); btnloop.seticon( new imageicon( "images2/4.png" )); //添加到框架 getcontentpane().add(btnlast); getcontentpane().add(btnplay); getcontentpane().add(btnnext); getcontentpane().add(btnloop); //添加監(jiān)聽 mymouselistener mml = new mymouselistener(); btnlast.addmouselistener(mml); btnplay.addmouselistener(mml); btnnext.addmouselistener(mml); btnloop.addmouselistener(mml); //歌曲列表的標題 jlabel listlabel = new jlabel( "播放列表" ); listlabel.setbounds( 10 , 120 , 100 , 30 ); font listlabelfont = new font( "黑體" ,font.bold, 16 ); listlabel.setfont(listlabelfont); getcontentpane().add(listlabel); //歌曲列表 /* songslist = new jlist(); songslist.setbounds(10, 150, 250, 300); songslist.setbackground(color.cyan); //把所有歌曲名逐個添加到list中 //songslist.setlistdata(strsongnames); for(int i=0;i<strsongnames.length;i++){ defaultlistmodel dm = (defaultlistmodel)songslist.getmodel(); dm.add(i,strsongnames[i]); } getcontentpane().add(songslist); */ defaultlistmodel lm = new defaultlistmodel(); songslist = new jlist(lm); songslist.setbounds( 10 , 150 , 250 , 300 ); songslist.setbackground(color.cyan); //把所有歌曲名逐個添加到list中 //songslist.setlistdata(strsongnames); songs = new audioclip[strsongnames.length]; for ( int i= 0 ;i<strsongnames.length;i++){ lm.add(i,strsongnames[i]); songs[i] = loadsound(strsongnames[i]); } getcontentpane().add(songslist); //lm.remove(3); //對jlist控件的監(jiān)聽技術(shù)實現(xiàn) songslist.addlistselectionlistener( new listselectionlistener() { @override public void valuechanged(listselectionevent e) { currentsong.stop(); index = songslist.getselectedindex(); isplayorstop = true ; playerthread = new thread( new musicrun() ); playerthread.start(); } }); //單開一個線程,專用于播放音樂 playerthread = new thread( new musicrun() ); playerthread.start(); setvisible( true ); } private audioclip loadsound(string filename) { try { url url = new url( "file:songs\\" +filename); audioclip au = applet.newaudioclip(url); return au; } catch (exception e) { e.printstacktrace(); } return null ; } //講解音樂播放的基本技術(shù) private void hello() { try { file f = new file( "songs\\song1.wav" ); url url = f.touri().tourl(); //url url = new url("file:songs\\song1.wav"); audioclip au = applet.newaudioclip(url); au.play(); //au.loop(); //au.stop(); } catch (exception e) { e.printstacktrace(); } } private class mymouselistener extends mouseadapter{ @override public void mouseclicked(mouseevent e) { jbutton btn = (jbutton) e.getsource(); currentsong.stop(); if (btn==btnplay){ isplayorstop = !isplayorstop; } else if (btn==btnlast){ index--; if (index< 0 ){ index = strsongnames.length- 1 ; } //isplayorstop=true; } else if (btn==btnnext){ index++; index = index%strsongnames.length; } else if (btn==btnloop){ isloop = !isloop; } if (isplayorstop){ //播放 playerthread = new thread( new musicrun() ); playerthread.start(); } else { //停止 songslist.setselectedindex(index); songnamelabel.settext( "停止播放:" +strsongnames[index]); btnplay.seticon( new imageicon( "images2/2.png" )); } } } private class musicrun implements runnable{ @override public void run() { currentsong = songs[index]; if (isloop){ currentsong.loop(); songnamelabel.settext( "循環(huán)播放:" +strsongnames[index]); } if (isplayorstop) { currentsong.play(); } //在播放列表中選定當前歌曲 songslist.setselectedindex(index); //把播放按鈕的圖標切換成“停止” btnplay.seticon( new imageicon( "images2/5.png" )); if (!isloop){ songnamelabel.settext( "正在播放:" +strsongnames[index]); } } } public static void main(string[] args) { new musicplayer(); } } |
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望通過本文能幫助到大家,謝謝大家對本站的支持!
原文鏈接:http://blog.csdn.net/weixin_37720904/article/details/71743170