4Java课程设计报告.docx
- 文档编号:13118111
- 上传时间:2023-06-11
- 格式:DOCX
- 页数:19
- 大小:189.24KB
4Java课程设计报告.docx
《4Java课程设计报告.docx》由会员分享,可在线阅读,更多相关《4Java课程设计报告.docx(19页珍藏版)》请在冰点文库上搜索。
4Java课程设计报告
《JAVA程序设计》课程设计报告
题目:
音乐播放器
学院:
计算机科学与工程学院
专业:
信息管理与信息系统
学生姓名:
杨柏昌
学号:
1400320228
指导老师:
汪华登
目录
1.概要设计....................................................................
2.详细设计....................................................................
3.测试............................................................................
总结............................................................................
概要设计
播放器主要使用JMF框架实现,播放器界面如下
详细设计
packagedesign.curriculum.JAVA;
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
importjavax.swing.border.Border;
importjava.io.*;
importjavax.media.*;
publicclassJMFPlayerextendsJFrameimplementsActionListener,ControllerListener{
Playerplayer=null;//声明一个Player对象
StringfilePath;//存放音乐文件夹所在目录
StringfileName;//存放歌曲名称
booleanclick=false;//用来标记歌曲是否被点击
intsongIndex;//歌曲元素在列表中的位置
intloop=3;//作为播放模式的标记
JLabelsongNameLable;
JRadioButtonsequenceBtn;
JRadioButtonsingleBtn;
JRadioButtonrandomBtn;
ButtonGrouploopGroup;
JButtonbeforeBtn;
JButtonnextBtn;
JButtonplayBtn;
JButtonpauseBtn;
JButtonstopBtn;
JButtonfileBtn;
JButtonexitBtn;
ComponentcontrolComponent;//player的控制面板
Containercontainer;
JPanelsongNamejp;
JPanelloopjp;
JPanelbtnjp;
JPanellistjp;
JPanelbtn2jp;
Listlist;
publicJMFPlayer(){
super("音乐播放器");
setBounds(480,50,400,600);
setResizable(false);//设置界面不可修改
setVisible(true);
container=getContentPane();//获得容器主面板
container.setLayout(newBoxLayout(container,BoxLayout.Y_AXIS));
songNamejp=newJPanel(newGridLayout(2,2));
songNamejp.setSize(400,50);
container.add(songNamejp);
songNameLable=newJLabel("正在播放:
未选取歌曲");
songNamejp.add(songNameLable);
songNamejp.setBorder(BorderFactory.createEtchedBorder());
loopjp=newJPanel();
loopjp.setSize(400,50);
container.add(loopjp);
sequenceBtn=newJRadioButton("顺序播放");
sequenceBtn.addActionListener(
newActionListener(){
publicvoidactionPerformed(ActionEventevt){
loop=1;
}
});
singleBtn=newJRadioButton("单曲循环");
singleBtn.addActionListener(
newActionListener(){
publicvoidactionPerformed(ActionEventevt){
loop=0;
}
});
randomBtn=newJRadioButton("随机播放");
randomBtn.addActionListener(
newActionListener(){
publicvoidactionPerformed(ActionEventevt){
loop=2;
}
});
loopGroup=newButtonGroup();
loopGroup.add(sequenceBtn);
loopGroup.add(singleBtn);
loopGroup.add(randomBtn);
loopjp.add(sequenceBtn);
loopjp.add(singleBtn);
loopjp.add(randomBtn);
btnjp=newJPanel();
btnjp.setLayout(newBoxLayout(btnjp,BoxLayout.X_AXIS));
btnjp.setSize(400,50);
container.add(btnjp);
beforeBtn=newJButton("上一首");
playBtn=newJButton("播放");
pauseBtn=newJButton("暂停");
stopBtn=newJButton("停止");
nextBtn=newJButton("下一首");
beforeBtn.addActionListener(this);
playBtn.addActionListener(this);
pauseBtn.addActionListener(this);
stopBtn.addActionListener(this);
nextBtn.addActionListener(this);
btnjp.add(beforeBtn);
btnjp.add(playBtn);
btnjp.add(pauseBtn);
btnjp.add(stopBtn);
btnjp.add(nextBtn);
listjp=newJPanel(newBorderLayout());
listjp.setSize(300,300);
container.add(listjp);
Borderborder=BorderFactory.createEtchedBorder();
Bordertitled=BorderFactory.createTitledBorder(border,"歌曲列表");
listjp.setBorder(titled);
fileBtn=newJButton("选择文件夹");
exitBtn=newJButton("退出");
fileBtn.addActionListener(this);
exitBtn.addActionListener(this);
btn2jp=newJPanel();
btnjp.setSize(300,50);
container.add(btn2jp);
btn2jp.add(fileBtn);
btn2jp.add(exitBtn);
list=newList();
list.setBackground(Color.pink);
JScrollPanesongListScroll=newJScrollPane(list);
listjp.add(songListScroll,BorderLayout.CENTER);
list.addMouseListener(newMouseAdapter(){//对歌曲点击的监听
publicvoidmouseClicked(MouseEvente){
if(e.getClickCount()==1){
fileName=list.getSelectedItem();
songIndex=list.getSelectedIndex();
click=true;
}
}
});
addWindowListener(newWindowAdapter()
{
publicvoidwindowClosing(WindowEvente)
{
if(player!
=null)
{
player.stop();player.deallocate();
}
System.exit(0);
}
}
);
validate();
}
/*
*对按钮事件的处理
*/
publicvoidactionPerformed(ActionEvente){
if(e.getSource()==fileBtn){
FileDialogdialog=newFileDialog(this,"音乐",FileDialog.LOAD);
dialog.setVisible(true);
filePath=dialog.getDirectory();
if(filePath!
=null){
Filefiledir=newFile(filePath);
File[]filelist=filedir.listFiles();
for(Filefile:
filelist){
Stringfilename=file.getName().toLowerCase();
if(filename.endsWith(".wav")||filename.endsWith(".mp3")){
list.add(filename);
}
}
}
}
if(e.getSource()==playBtn){
if(click){
click=!
click;
try
{
if(player!
=null)
{
player.stop();
player.deallocate();
}
if(controlComponent!
=null){
songNamejp.remove(controlComponent);
songNamejp.validate();
}
player=Manager.createPlayer(newMediaLocator("file:
"+filePath+fileName));
player.getDuration();
player.prefetch();
player.addControllerListener(this);
songNameLable.setText(fileName);
}
catch(IOExceptione3){
}
catch(NoPlayerExceptione2){
}
}
elseif(!
click){
player.start();
}
}
if(e.getSource()==exitBtn){
System.exit(0);
}
if(e.getSource()==pauseBtn){
if(player!
=null)
{
player.stop();
}
}
if(e.getSource()==stopBtn){
if(player!
=null)
{
player.stop();
player.setMediaTime(newTime(0));
}
else
return;
}
if(e.getSource()==beforeBtn){
intn=list.getItemCount();
if(songIndex songIndex=songIndex-1; if(songIndex<0){ songIndex=n-1; } fileName=list.getItem(songIndex); run(); } } if(e.getSource()==nextBtn){ intn=list.getItemCount(); if(songIndex songIndex=songIndex+1; if(songIndex>=n){ songIndex=0; } fileName=list.getItem(songIndex); run(); } } } publicvoidrun() { try { if(player! =null) { player.stop(); player.deallocate(); } if(controlComponent! =null){ songNamejp.remove(controlComponent); songNamejp.validate(); } player=Manager.createPlayer(newMediaLocator("file: "+filePath+fileName)); player.getDuration(); player.prefetch(); player.addControllerListener(this); songNameLable.setText(fileName); } catch(IOExceptione3){ } catch(NoPlayerExceptione2){ } } /* *对控制面板事件的处理 **/ publicsynchronizedvoidcontrollerUpdate(ControllerEventevent) { if(eventinstanceofRealizeCompleteEvent) { if((controlComponent=player.getControlPanelComponent())! =null) songNamejp.add(controlComponent); } elseif(eventinstanceofPrefetchCompleteEvent) { player.start(); } elseif(eventinstanceofEndOfMediaEvent) { player.setMediaTime(newTime(0)); if(loop==0){ player.start(); } if(loop==1){ intn=list.getItemCount(); if(songIndex fileName=list.getItem(songIndex+1); songIndex=songIndex+1; run(); } } if(loop==2){ intn=list.getItemCount(); if(songIndex songIndex=(int)(Math.random()*1000)%n; fileName=list.getItem(songIndex); run(); } } } validate(); } /* *主程序 */ publicstaticvoidmain(Stringsrgs[]){ newJMFPlayer(); } } 测试 1.运行程序如下: 2.点击文件夹按钮 弹出文件对话框,选择音乐文件夹 界面如下: 3.点击一首歌曲并播放 总结 做这个播放器用了两周的时间,一开始去图书馆借了好些书,发现了好几种类型做播放器用的API,发现有些比较繁杂和功能性不强,最终选用了JMF。 从书上和网上学了些JMF的知识,做个播放器难度不大。 在做到对于播放模式的实现时卡了一下,最终参考了其他播放器的做法,用了loop这个标记来实现播放模式的转换。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java 课程设计 报告