一个简单的Java音乐播放器
程序员文章站
2023-12-21 17:59:41
本文实例为大家分享了java音乐播放器展示的具体代码,供大家参考,具体内容如下
package kkmusic;
import java.applet.a...
本文实例为大家分享了java音乐播放器展示的具体代码,供大家参考,具体内容如下
package kkmusic; import java.applet.applet; import java.applet.audioclip; import java.awt.borderlayout; import java.awt.eventqueue; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.border.emptyborder; import java.awt.cardlayout; import javax.swing.jbutton; import javax.swing.jfilechooser; import javax.sound.sampled.floatcontrol; import javax.sound.sampled.sourcedataline; import javax.swing.grouplayout; import javax.swing.grouplayout.alignment; import java.awt.event.actionlistener; import java.awt.event.actionevent; import javax.swing.layoutstyle.componentplacement; import javax.swing.jmenubar; import javax.swing.jmenuitem; import java.awt.event.itemlistener; import java.io.file; import java.net.malformedurlexception; import java.net.url; import java.awt.event.itemevent; import javax.swing.jmenu; import javax.swing.jtextfield; import javax.swing.swingconstants; import javax.swing.jslider; import java.awt.event.mousemotionadapter; import java.awt.event.mouseevent; public class mp3 extends jframe { private jpanel contentpane; file file;//声明文件对象 string filename; jfilechooser chooser=new jfilechooser();//创建一个文件选择器 private jtextfield xiaoxi; boolean loop=false; audioclip adc;//声音音频剪辑对象 sourcedataline line; private floatcontrol volume = null; /** * launch the application. */ public static void main(string[] args) { eventqueue.invokelater(new runnable() { public void run() { try { mp3 frame = new mp3(); frame.setvisible(true); } catch (exception e) { e.printstacktrace(); } } }); } /** * create the frame. */ public mp3() { settitle("\u97f3\u4e50\u64ad\u653e\u5668"); setdefaultcloseoperation(jframe.exit_on_close); setbounds(100, 100, 265, 333); jmenubar menubar = new jmenubar(); setjmenubar(menubar); jmenu mnnewmenu = new jmenu("\u6587\u4ef6"); menubar.add(mnnewmenu); xiaoxi = new jtextfield(); xiaoxi.setcolumns(10); xiaoxi.settext("欢迎使用本播放器"); jmenuitem dakai = new jmenuitem("\u6253\u5f00"); dakai.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { int value=chooser.showopendialog(chooser);//接受文件选择器的状态 if(value==chooser.approve_option){ file=chooser.getselectedfile();//返回选中文件 filename=file.getname(); string flag=filename; xiaoxi.settext(flag); try { if(adc!=null) adc.stop(); url url=new url("file:"+file.getpath());//创建资源定位 adc=applet.newaudioclip(url); //adc.play(); } catch (malformedurlexception e1) { // todo auto-generated catch block e1.printstacktrace(); system.out.println("不能播放!"); } } } }); mnnewmenu.add(dakai); jmenuitem tuichu = new jmenuitem("\u9000\u51fa"); tuichu.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { dispose(); if(adc!=null) adc.stop(); return ; } }); mnnewmenu.add(tuichu); contentpane = new jpanel(); contentpane.setborder(new emptyborder(5, 5, 5, 5)); setcontentpane(contentpane); jbutton playbtn = new jbutton("\u64ad\u653e"); playbtn.sethorizontalalignment(swingconstants.left); playbtn.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { string flag="正在播放:"+filename; if(adc==null){ flag="请选择播放的音乐"; xiaoxi.settext(flag); return; } adc.play(); xiaoxi.settext(flag); } }); jbutton stopbtn = new jbutton("\u6682\u505c"); stopbtn.sethorizontalalignment(swingconstants.left); stopbtn.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { adc.stop(); string flag="停止播放音乐:"+filename; xiaoxi.settext(flag); } }); jbutton againbtn = new jbutton("\u5faa\u73af"); againbtn.sethorizontalalignment(swingconstants.left); againbtn.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { loop =!loop; string flag=""; ; if(loop){ adc.play(); adc.loop();//循环播放 flag="循环播放:"+filename; } else{ adc.play(); flag="顺序播放"+filename; } xiaoxi.settext(flag); } }); grouplayout gl_contentpane = new grouplayout(contentpane); gl_contentpane.sethorizontalgroup( gl_contentpane.createparallelgroup(alignment.leading) .addgroup(gl_contentpane.createsequentialgroup() .addcontainergap() .addgroup(gl_contentpane.createparallelgroup(alignment.trailing, false) .addcomponent(xiaoxi, alignment.leading) .addgroup(alignment.leading, gl_contentpane.createsequentialgroup() .addcomponent(playbtn, grouplayout.preferred_size, 64, grouplayout.preferred_size) .addpreferredgap(componentplacement.unrelated) .addcomponent(stopbtn, grouplayout.preferred_size, 66, grouplayout.preferred_size) .addpreferredgap(componentplacement.unrelated) .addcomponent(againbtn, grouplayout.preferred_size, 64, grouplayout.preferred_size))) .addcontainergap(15, short.max_value)) ); gl_contentpane.setverticalgroup( gl_contentpane.createparallelgroup(alignment.trailing) .addgroup(gl_contentpane.createsequentialgroup() .addcomponent(xiaoxi, grouplayout.preferred_size, grouplayout.default_size, grouplayout.preferred_size) .addpreferredgap(componentplacement.unrelated) .addpreferredgap(componentplacement.related, 173, short.max_value) .addgroup(gl_contentpane.createparallelgroup(alignment.baseline) .addcomponent(playbtn) .addcomponent(stopbtn) .addcomponent(againbtn)) .addcontainergap()) ); contentpane.setlayout(gl_contentpane); } }
运行结果如下:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。