1、2Java课程设计源代码package ui;import java.awt.EventQueue; /线程分发import javax.swing.JDialog;import javax.swing.JFrame;import javax.swing.UIManager;import javax.swing.UnsupportedLookAndFeelException;import org.jvnet.substance.SubstanceLookAndFeel;import musicPlayer.MusicPanel;public class Main public static v
2、oid main(String args) try / 设置观感 UIManager .setLookAndFeel(org.jvnet.substance.skin.SubstanceBusinessBlackSteelLookAndFeel); / 设置水印 SubstanceLookAndFeel .setCurrentWatermark(org.jvnet.substance.watermark.SubstanceMangoLookAndFeel); / 设置渐变渲染 SubstanceLookAndFeel .setCurrentGradientPainter(org.jvnet.s
3、ubstance.painter.WaveGradientPainter); JFrame.setDefaultLookAndFeelDecorated(true); /窗体 JDialog.setDefaultLookAndFeelDecorated(true); /对话框 catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) e.printStackTrace(); /e为对象引用的名称 /在命令行打印异常信息在
4、程序中出错的位置及原因 EventQueue.invokeLater() - /调用完毕,自动销毁(因为是一个匿名类) new MusicPanel(); ); package musicPlayer;import java.awt.BorderLayout;import java.awt.Color;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.File;import java.util.Vector;import javax.swing.JButton;import
5、 javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.filechooser.FileNameExtensionFilter;import javax.swing.table.DefaultTableModel;import player.Player;public class MusicPanel extends JFrame /* * */
6、 private static final long serialVersionUID = 1L; private JButton add, playbtn, stopbtn, deletebtn, deleteAllbtn, upbtn, downbtn; private JTable table; / 歌曲信息表 private Player player; public MusicPanel() super(1400320223音乐播放器); initCompont(); setVisible(true); /* * 初始化界面 * */ private void initCompont
7、() /各个按钮赋初始值 add = new JButton(导入); playbtn = new JButton(试听); stopbtn = new JButton(停止); deletebtn = new JButton(单曲删除); deleteAllbtn = new JButton(全部删除); upbtn = new JButton(上移); downbtn = new JButton(下移); / 导入按钮点击设置 add.addActionListener(new ActionListener() public void actionPerformed(ActionEvent
8、 e) addFile(); ); / 试听按钮点击设置 playbtn.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) if (player != null) player.stop(); player = null; int rowNum = table.getSelectedRow(); / 选中行时返回-1 if (rowNum != -1) / getValueAt获取表格内容 player = new Player(String) table.getValueAt(r
9、owNum, 1) + , (String) table.getValueAt(rowNum, 0); System.out.printf(String) table.getValueAt(rowNum, 1) + , (String) table.getValueAt(rowNum, 0); player.play(); ); / 停止按钮点击设置 stopbtn.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) if (player != null) player.stop()
10、; player = null; ); / 单曲删除按钮点击设置 deletebtn.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) int rowNum = table.getSelectedRow(); if (rowNum != -1) (DefaultTableModel) table.getModel().removeRow(rowNum); ); / 删除全部按钮点击设置 deleteAllbtn.addActionListener(new ActionListene
11、r() public void actionPerformed(ActionEvent e) for (int i = table.getRowCount() - 1; i = 0; i-) (DefaultTableModel) table.getModel().removeRow(i); ); downbtn.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) int n = table.getSelectedRow() + 1; if (n table.getRowCount(
12、) table.setRowSelectionInterval(n, n); ); upbtn.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) int n = table.getSelectedRow() - 1; if (n = 0) table.setRowSelectionInterval(n, n); ); / 添加各个按钮 JPanel btnPanel = new JPanel(); btnPanel.add(add); btnPanel.add(playbtn);
13、btnPanel.add(stopbtn); btnPanel.add(deletebtn); btnPanel.add(deleteAllbtn); btnPanel.add(upbtn); btnPanel.add(downbtn); this.setLayout(new BorderLayout(); this.add(btnPanel, BorderLayout.NORTH); Vector tableContent = new Vector(); Vector columnName = new Vector(); columnName.add(歌曲名称); columnName.ad
14、d(存放路径); / 设置table table = new JTable(tableContent, columnName); table.setSelectionBackground(Color.blue); table.setSelectionForeground(Color.LIGHT_GRAY); this.add(new JScrollPane(table), BorderLayout.CENTER); this.setSize(600, 210); /* * 添加文件 * */ private void addFile() JFileChooser fc = new JFileC
15、hooser(); / 设置选入文件类型 FileNameExtensionFilter filter = new FileNameExtensionFilter( mp3 or wav file, mp3, wav, MP3, WAV); fc.setFileFilter(filter); fc.setFileSelectionMode(JFileChooser.FILES_ONLY); / 设置只选文件 fc.setMultiSelectionEnabled(true); / 设置选择多个文件 int intRetVal = fc.showDialog(this, 打开); / 获取文件并
16、添加到table if (intRetVal = JFileChooser.APPROVE_OPTION) File file = fc.getSelectedFiles(); String name; for (File var : file) name = var.getName().toLowerCase(); if (name.endsWith(.mp3) | name.endsWith(.wav) this.addMusicItem(var.getName(), var.getParentFile() .getAbsolutePath(); /* * table的行中添加音乐文件名称
17、name,音乐文件路径path*/ private void addMusicItem(String name, String path) Vector rowData = new Vector(); rowData.add(name); rowData.add(path); DefaultTableModel tabMod = (DefaultTableModel) table.getModel(); tabMod.addRow(rowData); public static void main(String args) new MusicPanel(); package player;im
18、port java.io.File;import java.io.IOException;import javax.sound.sampled.AudioFormat;import javax.sound.sampled.AudioInputStream;import javax.sound.sampled.AudioSystem;import javax.sound.sampled.DataLine;import javax.sound.sampled.SourceDataLine;public class Player private String path; / 文件路径 private
19、 String name; / 文件名称 private AudioFormat audioFormat; / 播放格式 private AudioInputStream audioInputStream; / 音乐播放输入流 private SourceDataLine sourceDataLine; / 播放设备 private boolean isStop = false; / 播放停止标志 /* * 创建对象时需要传入路径及文件名称 * */ public Player(String path,String name) this.path = path; / 代表该构造方法所创建的对象
20、 this.name = name; /* * 播放音乐 * */ public void play() File file = new File(path + name); try / 获取音乐播package player;import java.io.File;import java.io.IOException;import javax.sound.sampled.AudioFormat;import javax.sound.sampled.AudioInputStream;import javax.sound.sampled.AudioSystem;import javax.soun
21、d.sampled.DataLine;import javax.sound.sampled.SourceDataLine;public class Player private String path; / 文件路径 private String name; / 文件名称 private AudioFormat audioFormat; / 播放格式 private AudioInputStream audioInputStream; / 音乐播放输入流 private SourceDataLine sourceDataLine; / 播放设备 private boolean isStop =
22、 false; / 播放停止标志 /* * 创建对象时需要传入路径及文件名称 * */ public Player(String path,String name) this.path = path; / 代表该构造方法所创建的对象 this.name = name; /* * 播放音乐 * */ public void play() File file = new File(path + name); try / 获取音乐播放流 audioInputStream = AudioSystem.getAudioInputStream(file); / 获取播放格式 audioFormat = a
23、udioInputStream.getFormat(); / 其他格式音乐文件处理 if (audioFormat.getEncoding() != AudioFormat.Encoding.PCM_SIGNED); audioFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,audioFormat.getSampleRate(), 16,audioFormat.getChannels(),audioFormat.getChannels()*2, audioFormat.getSampleRate(),false); audioI
24、nputStream = AudioSystem.getAudioInputStream(audioFormat,audioInputStream); / 打开输出设备 DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, audioFormat,AudioSystem.NOT_SPECIFIED); sourceDataLine = (SourceDataLine)AudioSystem.getLine(dataLineInfo); sourceDataLine.open(audioFormat); sour
25、ceDataLine.start(); / 启动播放线程 new Thread() public void run() try int n=0; byte tempBuffer = new byte1024; while (n!=-1) / 停止播放入口,如果isStop被设置为真,结束播放 if(isStop) break; / 将音乐输入流的数据读入tempBuffer缓存 n = audioInputStream.read(tempBuffer,0,tempBuffer.length); if (n0) / 将缓存数据写入播放设备,开始播放 sourceDataLine.write(te
26、mpBuffer,0 ,n); audioInputStream.close(); sourceDataLine.drain(); sourceDataLine.close(); catch(IOException e) e.printStackTrace(); throw new RuntimeException(); .start(); catch(Exception e) e.printStackTrace(); System.exit(0); throw new RuntimeException(); /* * 停止播放 * */ public void stop() try isSt
27、op = true; audioInputStream.close(); sourceDataLine.drain(); sourceDataLine.close(); catch(IOException e) e.printStackTrace(); 放流 audioInputStream = AudioSystem.getAudioInputStream(file); / 获取播放格式 audioFormat = audioInputStream.getFormat(); / 其他格式音乐文件处理 if (audioFormat.getEncoding() != AudioFormat.E
28、ncoding.PCM_SIGNED); audioFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,audioFormat.getSampleRate(), 16,audioFormat.getChannels(),audioFormat.getChannels()*2, audioFormat.getSampleRate(),false); audioInputStream = AudioSystem.getAudioInputStream(audioFormat,audioInputStream); / 打开输出设备 DataLine.Info dataLineInf
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1