2Java课程设计源代码.docx

上传人:b****0 文档编号:17599212 上传时间:2023-07-27 格式:DOCX 页数:16 大小:18.65KB
下载 相关 举报
2Java课程设计源代码.docx_第1页
第1页 / 共16页
2Java课程设计源代码.docx_第2页
第2页 / 共16页
2Java课程设计源代码.docx_第3页
第3页 / 共16页
2Java课程设计源代码.docx_第4页
第4页 / 共16页
2Java课程设计源代码.docx_第5页
第5页 / 共16页
2Java课程设计源代码.docx_第6页
第6页 / 共16页
2Java课程设计源代码.docx_第7页
第7页 / 共16页
2Java课程设计源代码.docx_第8页
第8页 / 共16页
2Java课程设计源代码.docx_第9页
第9页 / 共16页
2Java课程设计源代码.docx_第10页
第10页 / 共16页
2Java课程设计源代码.docx_第11页
第11页 / 共16页
2Java课程设计源代码.docx_第12页
第12页 / 共16页
2Java课程设计源代码.docx_第13页
第13页 / 共16页
2Java课程设计源代码.docx_第14页
第14页 / 共16页
2Java课程设计源代码.docx_第15页
第15页 / 共16页
2Java课程设计源代码.docx_第16页
第16页 / 共16页
亲,该文档总共16页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

2Java课程设计源代码.docx

《2Java课程设计源代码.docx》由会员分享,可在线阅读,更多相关《2Java课程设计源代码.docx(16页珍藏版)》请在冰点文库上搜索。

2Java课程设计源代码.docx

2Java课程设计源代码

packageui;

importjava.awt.EventQueue;//线程分发

importjavax.swing.JDialog;

importjavax.swing.JFrame;

importjavax.swing.UIManager;

importjavax.swing.UnsupportedLookAndFeelException;

importorg.jvnet.substance.SubstanceLookAndFeel;

importmusicPlayer.MusicPanel;

publicclassMain{

publicstaticvoidmain(String[]args){

try{

//设置观感

UIManager

.setLookAndFeel("org.jvnet.substance.skin.SubstanceBusinessBlackSteelLookAndFeel");

//设置水印

SubstanceLookAndFeel

.setCurrentWatermark("org.jvnet.substance.watermark.SubstanceMangoLookAndFeel");

//设置渐变渲染

SubstanceLookAndFeel

.setCurrentGradientPainter("org.jvnet.substance.painter.WaveGradientPainter");

JFrame.setDefaultLookAndFeelDecorated(true);//窗体

JDialog.setDefaultLookAndFeelDecorated(true);//对话框

}catch(ClassNotFoundException|InstantiationException

|IllegalAccessException|UnsupportedLookAndFeelExceptione){

e.printStackTrace();//e为对象引用的名称

//在命令行打印异常信息在程序中出错的位置及原因

}

EventQueue.invokeLater(()->{//调用完毕,自动销毁(因为是一个匿名类)

newMusicPanel();

});

}

}

packagemusicPlayer;

importjava.awt.BorderLayout;

importjava.awt.Color;

importjava.awt.event.ActionEvent;

importjava.awt.event.ActionListener;

importjava.io.File;

importjava.util.Vector;

importjavax.swing.JButton;

importjavax.swing.JFileChooser;

importjavax.swing.JFrame;

importjavax.swing.JPanel;

importjavax.swing.JScrollPane;

importjavax.swing.JTable;

importjavax.swing.filechooser.FileNameExtensionFilter;

importjavax.swing.table.DefaultTableModel;

importplayer.Player;

publicclassMusicPanelextendsJFrame{

/**

*

*/

privatestaticfinallongserialVersionUID=1L;

privateJButtonadd,playbtn,stopbtn,deletebtn,deleteAllbtn,upbtn,

downbtn;

privateJTabletable;//歌曲信息表

privatePlayerplayer;

publicMusicPanel(){

super("1400320223音乐播放器");

initCompont();

setVisible(true);

}

/**

*初始化界面

**/

privatevoidinitCompont(){

// 各个按钮赋初始值

add=newJButton("导入");

playbtn=newJButton("试听");

stopbtn=newJButton("停止");

deletebtn=newJButton("单曲删除");

deleteAllbtn=newJButton("全部删除");

upbtn=newJButton("上移");

downbtn=newJButton("下移");

//导入按钮点击设置

add.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEvente){

addFile();

}

});

//试听按钮点击设置

playbtn.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEvente){

if(player!

=null){

player.stop();

player=null;

}

introwNum=table.getSelectedRow();//选中行时返回-1

if(rowNum!

=-1){

//getValueAt获取表格内容

player=newPlayer((String)table.getValueAt(rowNum,1)

+"\\",(String)table.getValueAt(rowNum,0));

System.out.printf((String)table.getValueAt(rowNum,1)

+"\\",(String)table.getValueAt(rowNum,0));

player.play();

}

}

});

//停止按钮点击设置

stopbtn.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEvente){

if(player!

=null){

player.stop();

player=null;

}

}

});

//单曲删除按钮点击设置

deletebtn.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEvente){

introwNum=table.getSelectedRow();

if(rowNum!

=-1){

((DefaultTableModel)table.getModel()).removeRow(rowNum);

}

}

});

//删除全部按钮点击设置

deleteAllbtn.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEvente){

for(inti=table.getRowCount()-1;i>=0;i--){

((DefaultTableModel)table.getModel()).removeRow(i);

}

}

});

downbtn.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEvente){

intn=table.getSelectedRow()+1;

if(n

table.setRowSelectionInterval(n,n);

}

}

});

upbtn.addActionListener(newActionListener(){

publicvoidactionPerformed(ActionEvente){

intn=table.getSelectedRow()-1;

if(n<-1){

n=table.getRowCount()-1;

}

if(n>=0){

table.setRowSelectionInterval(n,n);

}

}

});

//添加各个按钮

JPanelbtnPanel=newJPanel();

btnPanel.add(add);

btnPanel.add(playbtn);

btnPanel.add(stopbtn);

btnPanel.add(deletebtn);

btnPanel.add(deleteAllbtn);

btnPanel.add(upbtn);

btnPanel.add(downbtn);

this.setLayout(newBorderLayout());

this.add(btnPanel,BorderLayout.NORTH);

VectortableContent=newVector();

VectorcolumnName=newVector();

columnName.add("歌曲名称");

columnName.add("存放路径");

//设置table

table=newJTable(tableContent,columnName);

table.setSelectionBackground(Color.blue);

table.setSelectionForeground(Color.LIGHT_GRAY);

this.add(newJScrollPane(table),BorderLayout.CENTER);

this.setSize(600,210);

}

/**

*添加文件

**/

privatevoidaddFile(){

JFileChooserfc=newJFileChooser();

//设置选入文件类型

FileNameExtensionFilterfilter=newFileNameExtensionFilter(

"mp3orwavfile","mp3","wav","MP3","WAV");

fc.setFileFilter(filter);

fc.setFileSelectionMode(JFileChooser.FILES_ONLY);//设置只选文件

fc.setMultiSelectionEnabled(true);//设置选择多个文件

intintRetVal=fc.showDialog(this,"打开");

//获取文件并添加到table

if(intRetVal==JFileChooser.APPROVE_OPTION){

File[]file=fc.getSelectedFiles();

Stringname;

for(Filevar:

file){

name=var.getName().toLowerCase();

if(name.endsWith(".mp3")||name.endsWith(".wav")){

this.addMusicItem(var.getName(),var.getParentFile()

.getAbsolutePath());

}

}

}

}

/**

*table的行中添加音乐文件名称name,音乐文件路径path*/

privatevoidaddMusicItem(Stringname,Stringpath){

VectorrowData=newVector();

rowData.add(name);

rowData.add(path);

DefaultTableModeltabMod=(DefaultTableModel)table.getModel();

tabMod.addRow(rowData);

}

publicstaticvoidmain(String[]args){

newMusicPanel();

}

}

packageplayer;

importjava.io.File;

importjava.io.IOException;

importjavax.sound.sampled.AudioFormat;

importjavax.sound.sampled.AudioInputStream;

importjavax.sound.sampled.AudioSystem;

importjavax.sound.sampled.DataLine;

importjavax.sound.sampled.SourceDataLine;

publicclassPlayer{

privateStringpath;//文件路径

privateStringname;//文件名称

privateAudioFormataudioFormat;//播放格式

privateAudioInputStreamaudioInputStream;//音乐播放输入流

privateSourceDataLinesourceDataLine;//播放设备

privatebooleanisStop=false;//播放停止标志

/**

*创建对象时需要传入路径及文件名称

**/

publicPlayer(Stringpath,Stringname){

this.path=path;//代表该构造方法所创建的对象

this.name=name;

}

/**

*播放音乐

**/

publicvoidplay(){

Filefile=newFile(path+name);

try{

//获取音乐播packageplayer;

importjava.io.File;

importjava.io.IOException;

importjavax.sound.sampled.AudioFormat;

importjavax.sound.sampled.AudioInputStream;

importjavax.sound.sampled.AudioSystem;

importjavax.sound.sampled.DataLine;

importjavax.sound.sampled.SourceDataLine;

publicclassPlayer{

privateStringpath;//文件路径

privateStringname;//文件名称

privateAudioFormataudioFormat;//播放格式

privateAudioInputStreamaudioInputStream;//音乐播放输入流

privateSourceDataLinesourceDataLine;//播放设备

privatebooleanisStop=false;//播放停止标志

/**

*创建对象时需要传入路径及文件名称

**/

publicPlayer(Stringpath,Stringname){

this.path=path;//代表该构造方法所创建的对象

this.name=name;

}

/**

*播放音乐

**/

publicvoidplay(){

Filefile=newFile(path+name);

try{

//获取音乐播放流

audioInputStream=AudioSystem.getAudioInputStream(file);

//获取播放格式

audioFormat=audioInputStream.getFormat();

//其他格式音乐文件处理

if(audioFormat.getEncoding()!

=AudioFormat.Encoding.PCM_SIGNED);{

audioFormat=newAudioFormat(AudioFormat.Encoding.PCM_SIGNED,audioFormat.getSampleRate(),

16,audioFormat.getChannels(),audioFormat.getChannels()*2,

audioFormat.getSampleRate(),false);

audioInputStream=AudioSystem.getAudioInputStream(audioFormat,audioInputStream);

}

//打开输出设备

DataLine.InfodataLineInfo=newDataLine.Info(SourceDataLine.class,audioFormat,AudioSystem.NOT_SPECIFIED);

sourceDataLine=(SourceDataLine)AudioSystem.getLine(dataLineInfo);

sourceDataLine.open(audioFormat);

sourceDataLine.start();

//启动播放线程

newThread(){

publicvoidrun(){

try{

intn=0;

bytetempBuffer[]=newbyte[1024];

while(n!

=-1){

//停止播放入口,如果isStop被设置为真,结束播放

if(isStop)break;

//将音乐输入流的数据读入tempBuffer缓存

n=audioInputStream.read(tempBuffer,0,tempBuffer.length);

if(n>0){

//将缓存数据写入播放设备,开始播放

sourceDataLine.write(tempBuffer,0,n);

}

}

audioInputStream.close();

sourceDataLine.drain();

sourceDataLine.close();

}

catch(IOExceptione){

e.printStackTrace();

thrownewRuntimeException();

}

}

}.start();

}catch(Exceptione){

e.printStackTrace();

System.exit(0);

thrownewRuntimeException();

}

}

/**

*停止播放

**/

publicvoidstop(){

try{

isStop=true;

audioInputStream.close();

sourceDataLine.drain();

sourceDataLine.close();

}

catch(IOExceptione){

e.printStackTrace();

}

}

}

放流

audioInputStream=AudioSystem.getAudioInputStream(file);

//获取播放格式

audioFormat=audioInputStream.getFormat();

//其他格式音乐文件处理

if(audioFormat.getEncoding()!

=AudioFormat.Encoding.PCM_SIGNED);{

audioFormat=newAudioFormat(AudioFormat.Encoding.PCM_SIGNED,audioFormat.getSampleRate(),

16,audioFormat.getChannels(),audioFormat.getChannels()*2,

audioFormat.getSampleRate(),false);

audioInputStream=AudioSystem.getAudioInputStream(audioFormat,audioInputStream);

}

//打开输出设备

DataLine.InfodataLineInf

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 表格模板 > 合同协议

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2