基于java的音乐播放器的设计源代码+实验报告.doc
《基于java的音乐播放器的设计源代码+实验报告.doc》由会员分享,可在线阅读,更多相关《基于java的音乐播放器的设计源代码+实验报告.doc(34页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、. .主类MUSICPLAYER类:import java.util.*;import javax.swing.JSlider;import java.awt.BorderLayout;import java.awt.FlowLayout;import java.awt.Point;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.io.Buff
2、eredOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;import javax.media.ControllerEvent;import
3、 javax.media.ControllerListener;import javax.media.EndOfMediaEvent;import javax.media.Manager;import javax.media.MediaLocator;import javax.media.NoPlayerException;import javax.media.Player;import javax.media.PrefetchpleteEvent;import javax.media.Time;import javax.swing.ButtonGroup;import javax.swing
4、.DefaultListModel;import javax.swing.ImageIcon;import javax.swing.utton;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JList;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JOptionPane;import javax.swing.JPanel;import
5、 javax.swing.JRadioButtonMenuItem;import javax.swing.JScrollBar;import javax.swing.JScrollPane;import javax.swing.filechooser.FileNameExtensionFilter;public class MusicPlayer implements ActionListener, Serializable,ControllerListenerprivate static final long serialVersionUID = 1L;private JFrame fram
6、e = null;private JPanel controlPanel = null;private utton btnPlay = null;private utton btnPre = null;private utton btnNext = null;private JScrollPane listPane = null;private JList list = null;private DefaultListModel listModel = null;private JMenuBar menubar = null;private JMenu menuFile = null, men
7、uAbout = null, menuMode = null;private JMenuItem itemOpen, itemOpens, itemExit, itemAbout;private JRadioButtonMenuItem itemSingle, itemSequence ,itemRandom;private ListItem currentItem = null;private static Player player = null;private boolean isPause = false;private int mode;private int currentInde
8、x;private ImageIcon iconPlay = new ImageIcon(d:1.jpg);private ImageIcon iconPre = new ImageIcon(d:3.jpg);private ImageIcon iconNext = new ImageIcon(d:2.jpg);private ImageIcon iconPause = new ImageIcon(d:4.jpg);public static void main(String args)new MusicPlayer();public MusicPlayer()init();public vo
9、id init()frame = new JFrame();frame.setTitle(音乐播放器);frame.setSize(400, 300);frame.setResizable(false);frame.setLocationRelativeTo(null);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);menubar = new JMenuBar();menuFile = new JMenu(文件);menuAbout = new JMenu(关于);menuMode = new JMenu(播放模式);itemOpen
10、 = new JMenuItem(添加文件);itemOpens = new JMenuItem(添加文件夹);itemExit = new JMenuItem(退出);itemAbout = new JMenuItem(关于);itemOpen.addActionListener(this);itemOpens.addActionListener(this);itemExit.addActionListener(this);itemAbout.addActionListener(this);itemSequence = new JRadioButtonMenuItem(顺序播放);itemS
11、equence.setSelected(true);itemSingle = new JRadioButtonMenuItem(单曲循环);itemSequence.addActionListener(this);itemRandom = new JRadioButtonMenuItem(随机播放);itemRandom.addActionListener(this);itemSingle.addActionListener(this);ButtonGroup bg = new ButtonGroup();bg.add(itemRandom);bg.add(itemSequence);bg.a
12、dd(itemSingle);menuFile.add(itemOpen);menuFile.add(itemOpens);menuFile.add(itemExit);menuAbout.add(itemAbout);menuMode.add(itemSequence);menuMode.add(itemSingle);menuMode.add(itemRandom);menubar.add(menuFile);menubar.add(menuAbout);menubar.add(menuMode);frame.setJMenuBar(menubar);frame.setLayout(new
13、 BorderLayout();controlPanel = new JPanel();controlPanel.setLayout(new FlowLayout();btnPlay = new utton(iconPlay);btnPre = new utton(iconPre);btnNext = new utton(iconNext);btnPlay.addActionListener(this);btnPre.addActionListener(this);btnNext.addActionListener(this);controlPanel.add(btnPre);controlP
14、anel.add(btnPlay);controlPanel.add(btnNext);listPane = new JScrollPane();listModel = load();list = new JList(listModel);if (list.getSelectedIndex() = -1 & listModel.size() 0)currentItem = (ListItem) listModel.get(0);list.setSelectedIndex(0);currentIndex=0;listPane.getViewport().add(list);list.addMou
15、seListener(new MouseAdapter()public void mouseClicked(MouseEvent e)if (e.getClickCount() = 2)if(player!=null)player.close();btnPlay.setIcon(iconPlay);currentIndex = list.locationToIndex(e.getPoint();currentItem = (ListItem) listModel.get(currentIndex);list.setSelectedIndex(currentIndex);play(););fra
16、me.setLayout(new BorderLayout();frame.add(controlPanel, BorderLayout.NORTH);frame.add(listPane, BorderLayout.CENTER);frame.setVisible(true);public void actionPerformed(ActionEvent e)if (e.getSource() = itemOpen)/ add filesJFileChooser jfc = new JFileChooser();FileNameExtensionFilter filter = new Fil
17、eNameExtensionFilter(音乐文件, mp3, wav);jfc.setFileFilter(filter);jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);jfc.setMultiSelectionEnabled(true);if (jfc.showOpenDialog(frame) = JFileChooser.APPROVE_OPTION)File files = jfc.getSelectedFiles();for (File f : files)ListItem item = new ListItem(f.getNa
18、me(), f.getAbsolutePath();listModel.addElement(item); else if (e.getSource() = itemOpens)/ add files in a directoryJFileChooser jfc = new JFileChooser();jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);if (jfc.showOpenDialog(frame) = JFileChooser.APPROVE_OPTION)File directory = jfc.getSelecte
19、dFile();File files = directory.listFiles(new java.io.FileFilter()public boolean accept(File f)if (f.getName().toLowerCase().endsWith(.mp3)| f.getName().toLowerCase().endsWith(.wav)return true;return false;);for (File file : files)ListItem item = new ListItem(file.getName(), file.getAbsolutePath();li
20、stModel.addElement(item);save(listModel); else if (e.getSource() = itemExit)System.exit(0); else if (e.getSource() = itemAbout)JOptionPane.showMessageDialog(frame, 作者:陆鑫); else if (e.getSource() = btnPlay)/ play or pauseplay(); else if (e.getSource() = btnPre)pre(); else if (e.getSource() = btnNext)
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 基于 java 音乐 播放 设计 源代码 实验 报告
限制150内