首页

java 音乐播放器 实例源码

java

2020-7-7

package gui;

import java.awt.*;

import javax.swing.*;
import javax.swing.border.EmptyBorder;

import file.ChooseFile;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.net.MalformedURLException;

import test.*;

public class MainJFrame extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	JMenuBar menuBar;
	JButton []textArea;	
	MusicList song;
	String musicPath=null;

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainJFrame frame = new MainJFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 * @throws IOException 
	 */
	public MainJFrame() throws IOException {
		setIconImage(new ImageIcon("title.png").getImage());//设置图标
		setResizable(false);//禁止拉伸窗口
		//setAutoRequestFocus(false);
		setTitle("KuDog");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭按钮的操作
		setBounds(100, 100, 293, 489);//位置、尺寸
		setAlwaysOnTop(true);//置顶
		
		File saveMusicPath=new File("C:\\Program Files\\KuDog\\musicPath.dat");
		if(saveMusicPath.exists()){
			FileReader fr = new FileReader(saveMusicPath);
			BufferedReader br = new BufferedReader(fr);
			String s = br.readLine();
			musicPath = s;
			br.close();
		}			
		else
			musicPath=ChooseFile.getFile();//设置文件路径
		
		////////////////////////////////////
		song=new MusicList(new File(musicPath),".wav");
		
		menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu menu1 = new JMenu("文件");
		menuBar.add(menu1);
		
		JMenuItem menu1_1 = new JMenuItem("打开");
		menu1_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				
				song.musicList.add(ChooseFile.getFileName());
				song.isPlay  ;
				textField.setText(song.musicList.get(song.isPlay));
				try {
					song.songPlay();
				} catch (MalformedURLException e) {
					e.printStackTrace();
				}
			}
		});
		menu1.add(menu1_1);
		
		JMenuItem menu1_2 = new JMenuItem("退出");
		menu1_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				System.exit(0); //退出JVM
			}
		});
		menu1.add(menu1_2);
		
		JMenu menu2 = new JMenu("模式");
		menuBar.add(menu2);
		
		JMenuItem menu2_1 = new JMenuItem("单曲播放");
		menu2.add(menu2_1);
		
		JMenuItem menu2_2 = new JMenuItem("单曲循环");
		menu2.add(menu2_2);
		
		JMenuItem menu2_3 = new JMenuItem("顺序播放");
		menu2.add(menu2_3);
		
		JMenuItem menu2_4 = new JMenuItem("循环播放");
		menu2.add(menu2_4);
		
		JMenuItem menu2_5 = new JMenuItem("随机播放");
		menu2.add(menu2_5);
		
		JMenu menu3 = new JMenu("设置");
		menuBar.add(menu3);
		
		JMenuItem menu3_1 = new JMenuItem("目录");
		menu3_1.setToolTipText("重启后生效");
		menu3_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				try {
					musicPath=ChooseFile.getFile();
					FileWriter writer=new FileWriter("C:\\Program Files\\KuDog\\musicPath.dat");
					 writer.write(musicPath);
					 writer.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});
		menu3.add(menu3_1);
		
		JMenu menu4 = new JMenu("帮助");
		menuBar.add(menu4);
		
		JMenuItem menu4_1 = new JMenuItem("欢迎");
		menu4.add(menu4_1);
		
		JMenuItem menu4_2 = new JMenuItem("关于");
		menu4.add(menu4_2);
		
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);//空布局
				
		textField = new JTextField();
		textField.setEditable(false);//不能编辑
		textField.setBackground(Color.LIGHT_GRAY);//背景色
		textField.setBounds(0, 13, 293, 43);
		contentPane.add(textField);
		textField.setForeground(Color.WHITE);//前景色
		textField.setFont(new Font("黑体",Font.BOLD,18));//字体
		
		//上一曲
		final JButton lastButton = new JButton("上一曲");
		lastButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				try {
					song.songLast();
				} catch (Exception e) {
					e.printStackTrace();
				}
				textField.setText(song.musicList.get(song.isPlay));//歌名显示
			}
		});
		lastButton.setBounds(0, 77, 77, 27);
		contentPane.add(lastButton);
		
		//下一曲
		final JButton nextButton = new JButton("下一曲");
		nextButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				try {
					song.songNext();
				} catch (Exception e) {
					e.printStackTrace();
				}
				textField.setText(song.musicList.get(song.isPlay));//歌名显示
			}
		});
		nextButton.setBounds(211, 77, 77, 27);
		contentPane.add(nextButton);
		
		//播放
		final JButton playButton = new JButton("播放");
		playButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
					try {
						song.songPlay();
					} catch (Exception e) {
						e.printStackTrace();
					}
					textField.setText(song.musicList.get(song.isPlay));//歌名显示
			}
		});
		playButton.setBounds(78, 77, 66, 27);
		contentPane.add(playButton);
		
		//暂停
		final JButton stopButton = new JButton("暂停");
		stopButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				song.songStop();
			}
		});
		stopButton.setBounds(146, 77, 66, 27);
		contentPane.add(stopButton);
		
		textArea=new JButton[song.musicList.size()];
		
		//播放列表显示所有歌曲
		StringBuilder musicListText=new StringBuilder();
		for(int i=0;i<song.musicList.size()&&i<15;i  ){
			textArea[i] = new JButton();
			
			textArea[i].setText((i 1) "." song.musicList.get(i));
			textArea[i].setFont(new Font("宋体", Font.PLAIN, 15));
			textArea[i].setVisible(true);
			textArea[i].setBounds(20,20*(i 6),240,20);
			textArea[i].setBackground(Color.WHITE);
			textArea[i].setHorizontalAlignment(JButton.LEFT);//按钮文字左对齐
			textArea[i].addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent event) {
					try {
						JButton myButton=(JButton) event.getSource();
						
						//从Button获取text,然后截取前面数字-1
						song.isPlay=Integer.valueOf(myButton.getText().substring(0, myButton.getText().indexOf(".")))-1;
						song.songPlay();
					} catch (Exception e) {
						e.printStackTrace();
					}
					textField.setText(song.musicList.get(song.isPlay));//歌名显示
				}
			});
			contentPane.add(textArea[i]);
		}
	}
}

资源下载此资源下载价格为2D币(VIP免费),请先
资源文件列表
KuDogMusic/.classpath , 301
KuDogMusic/.project , 386
KuDogMusic/.settings/org.eclipse.jdt.core.prefs , 598
KuDogMusic/bin/file/ChooseFile.class , 1237
KuDogMusic/bin/file/MusicFile.class , 1675
KuDogMusic/bin/gui/MainJFrame$1.class , 713
KuDogMusic/bin/gui/MainJFrame$2.class , 1354
KuDogMusic/bin/gui/MainJFrame$3.class , 672
KuDogMusic/bin/gui/MainJFrame$4.class , 1098
KuDogMusic/bin/gui/MainJFrame$5.class , 1179
KuDogMusic/bin/gui/MainJFrame$6.class , 1179
KuDogMusic/bin/gui/MainJFrame$7.class , 1179
KuDogMusic/bin/gui/MainJFrame$8.class , 709
KuDogMusic/bin/gui/MainJFrame$9.class , 1630
KuDogMusic/bin/gui/MainJFrame.class , 5903
KuDogMusic/bin/test/MusicList.class , 2308
KuDogMusic/bin/test/MusicPlay.class , 1006
KuDogMusic/kudog.ico , 67646
KuDogMusic/MANIFEST.MF , 88
KuDogMusic/src/file/ChooseFile.class , 1013
KuDogMusic/src/file/ChooseFile.java , 838
KuDogMusic/src/file/MusicFile.java , 781
KuDogMusic/src/gui/MainJFrame$1.class , 559
KuDogMusic/src/gui/MainJFrame$2.class , 1179
KuDogMusic/src/gui/MainJFrame$3.class , 537
KuDogMusic/src/gui/MainJFrame$4.class , 890
KuDogMusic/src/gui/MainJFrame$5.class , 1015
KuDogMusic/src/gui/MainJFrame$6.class , 1015
KuDogMusic/src/gui/MainJFrame$7.class , 1015
KuDogMusic/src/gui/MainJFrame$8.class , 574
KuDogMusic/src/gui/MainJFrame$9.class , 1421
KuDogMusic/src/gui/MainJFrame.class , 5250
KuDogMusic/src/gui/MainJFrame.java , 6827
KuDogMusic/src/test/MusicList.class , 2113
KuDogMusic/src/test/MusicList.java , 1750
KuDogMusic/src/test/MusicPlay.class , 865
KuDogMusic/src/test/MusicPlay.java , 474
KuDogMusic/test.jar , 14943
KuDogMusic/title.png , 24602
KuDogMusic/u=165672687,1055598309&fm=21&gp=0.jpg , 8170
没有账号? 忘记密码?

社交账号快速登录