作者:虎爷在江湖 | 来源:互联网 | 2023-10-11 17:43
制作简易音乐播放器:
显示效果:
全部代码:
import java.applet.Applet;
import java.applet.AudioClip;
import java.net.MalformedURLException;
import java.net.URL;
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import java.util.Timer;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
class music{ //定义歌曲结构体public String musicPath; //歌曲路径public String musicName; //歌曲名public int musicTime; //歌曲时间
}
class MobileTool { //界面相应鼠标移动private Point OriginalPoint=null,MobileDistance=null; //鼠标移动前的位置和鼠标移动的距离private boolean isDraging = false; //用与判断鼠标书否松开public MobileTool(final Component frame){super();OriginalPoint=new Point(); //鼠标移动前的位置MobileDistance=new Point(); //鼠标移动的距离 /*确保取消标题栏以后,窗口仍然能接受鼠标点击事件*/frame.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { //按下鼠标时触发的动作isDraging = true;OriginalPoint.x = e.getX(); //获取鼠标移动前的位置OriginalPoint.y = e.getY();}public void mouseReleased(MouseEvent e) { //松开鼠标触发的事件isDraging = false;}});frame.addMouseMotionListener(new MouseMotionAdapter() { //鼠标移动时的动作public void mouseDragged(MouseEvent e) {if (isDraging) {MobileDistance= frame.getLocation(); //获取鼠标移动距离/*重新绘制位置*/frame.setLocation(MobileDistance.x + e.getX() - OriginalPoint.x, MobileDistance.y + e.getY() - OriginalPoint.y);} }});}
}class audioplay{//播放音乐类AudioClip adc;// 声音音频剪辑对象URL url;boolean adcFlag=false;boolean playFlag=false;void SetPlayAudioPath(String path){try{ url = new URL(path); // System.out.println(adc.toString());if(adcFlag==true){adc.stop();playFlag=false;}adc = Applet.newAudioClip(url);adcFlag=true;}catch (MalformedURLException e1) {e1.printStackTrace(); } }void play(){ //开始播放adc.play();playFlag=true;} void stop(){ //停止播放adc.stop(); playFlag=false;}
}
class MyExtendsJFrame extends JFrame implements ActionListener,MouseListener{private static final long serialVersionUID &#61; 1L;private static final int Font_PLAIN &#61; 0;private static final String regex&#61;"\\[\\d{2}:\\d{2}\\.\\d{2}\\]"; private static final String regex1&#61;"\\[\\d{2}:\\d{2}\\.\\d{2}\\]";//窗口类 LinkedList Music&#61;new LinkedList();//创建歌曲结构体为对象的链表audioplay audioPlay&#61;new audioplay(); //创建播放对象String playName,playDirectory,playPath,lyricsPath; //文件名&#xff0c;文件路径&#xff0c;音乐文件本身,单词文件本身int TIME; //歌曲时间备用int schemaflag&#61;0; //歌曲播放模式&#xff08;1单曲循环&#xff0c;0列表循环&#xff0c;2随机播放&#xff09;music m; //备用歌曲对象JLabel background;//背景控件JLabel cartoon;//播放动画JButton buttonPlay,buttonStop,buttonPrev,buttonNext;JButton buttonloop; //点击后循环播放JButton buttonunloop; //点击后随机播放JButton buttonrandom; //点击后列表循环JButton buttonlist; //点击后循环播放JButton buttonOpen,buttonList1,buttonList2,buttonexit,buttonmin;JTextPane textLyrics1,textLyrics2,textLyrics3;//歌词控件JTextPane time;JLabel playTime;//播放进度条控件JList listPlayFile;//播放列表控件Timer nTimer;//定时器对象Vector vt&#61;new Vector (); //创建Vector对象&#xff0c;通过add方法添加多行String lyc[]&#61;new String[100]; //存放歌词信息int time0[]&#61;new int[100]; //存放歌词对应的时间String time1[]&#61;new String[100];public MyExtendsJFrame(){setBounds(321,121,1277,795); //设置窗口大小 setLayout(null);//空布局 init(); //添加控件的操作封装成一个函数 setVisible(true);//放在添加组件后面执行setDefaultCloseOperation(EXIT_ON_CLOSE);dispose(); //去掉标题栏setUndecorated(true);setVisible(true);}public void play_init(String x,String y){audioPlay.SetPlayAudioPath("file:"&#43;y);//设置播放文件audioPlay.play();//开始播放timerFun();remove(textLyrics1);remove(textLyrics2);remove(textLyrics3);m&#61;new music(); //创建歌曲对象int j&#61;y.indexOf(".");StringBuilder term&#61;new StringBuilder(y);term.replace(j&#43;1,j&#43;4,"txt");lyricsPath&#61;new String(term);String lyric1&#61;""; //存放歌词的字符串形式String t&#61;""; //用来保存字符串try {File filename&#61;new File(lyricsPath);InputStreamReader reader&#61;new InputStreamReader(new FileInputStream(filename));//建立输入流对象BufferedReader buff&#61;new BufferedReader(reader);while((t&#61;buff.readLine())!&#61;null){lyric1&#61;lyric1&#43;t&#43;buff.readLine();}buff.close();}catch(Exception e1){e1.printStackTrace();}String lyric&#61;lyric1.substring(10); //方便计算lyc&#61;lyric.split(regex);//歌词取为歌词数组String text&#61;new String();for(int i&#61;0;i&#61;0;i--){if(Music.get(i).musicName.equals(playName)&&i&#61;&#61;0){break;}else if(Music.get(i).musicName.equals(playName)){s&#61;i-1;break;}}nTimer.cancel();playPath&#61;Music.get(s).musicPath;playName&#61;Music.get(s).musicName;play_init(playName,playPath);}else if(e.getSource()&#61;&#61;buttonNext){//下一首按钮事件int s&#61;0;for (int i&#61;Music.size()-1;i>&#61;0;i--){if(Music.get(i).musicName.equals(playName)&&i&#61;&#61;Music.size()-1){s&#61;i;break;}else if(Music.get(i).musicName.equals(playName)){s&#61;i&#43;1;break;}}nTimer.cancel();playPath&#61;Music.get(s).musicPath;playName&#61;Music.get(s).musicName;play_init(playName,playPath);}else if(e.getSource()&#61;&#61;buttonList1){//显示歌曲列表事件remove(buttonList1);listPlayFile.setVisible(true);buttonList2&#61;new JButton();buttonList2.setBorderPainted(false);buttonList2.setBounds(1180,696,30,30);Icon icon&#61;new ImageIcon(".//list1.jpg");buttonList2.setIcon(icon);add(buttonList2);buttonList2.addActionListener(this);validate();repaint();}else if(e.getSource()&#61;&#61;buttonList2){remove(buttonList2);listPlayFile.setVisible(false);buttonList1&#61;new JButton();buttonList1.setBorderPainted(false);buttonList1.setBounds(1180,696,30,30);Icon icon&#61;new ImageIcon(".//list1.jpg");buttonList1.setIcon(icon);add(buttonList1);buttonList1.addActionListener(this);validate();repaint();}}public void mousePressed(MouseEvent e){}public void mouseReleased(MouseEvent e){}public void mouseEntered(MouseEvent e){}public void mouseExited(MouseEvent e){}public void mouseClicked(MouseEvent e){}void init(){//添加的控件 Icon img&#61;new ImageIcon(".//background.jpg"); //创建图标对象 background &#61; new JLabel(img);//设置背景图片background.setBounds(0,0,1280,800);//设置背景控件大小getLayeredPane().add(background, new Integer(Integer.MIN_VALUE));//背景图片控件置于最底层((JPanel)getContentPane()).setOpaque(false); //控件透明img&#61;new ImageIcon(".//timg.gif");cartoon &#61;new JLabel(img);cartoon.setBounds(100,197,440,330);add(cartoon);buttonmin&#61;new JButton();//添加最小按钮buttonmin.setBorderPainted(false);buttonmin.setBounds(1175,22,40,31); //设置最小按钮大小Icon icon&#61;new ImageIcon(".//min1.jpg");//创建最小图标对象buttonmin.setIcon(icon); //设置最小图标add(buttonmin);//添加最小按钮至窗口buttonexit&#61;new JButton();buttonexit.setBorderPainted(false);buttonexit.setBounds(1220,20,38,30);icon&#61;new ImageIcon(".//exit1.jpg");buttonexit.setIcon(icon);add(buttonexit);buttonexit.addMouseListener(new MouseAdapter() { // 对buttonexit按钮添加监听事件&#64;Overridepublic void mouseClicked(MouseEvent e) { // 当鼠标点击时System.exit(0); // 退出}});buttonPlay&#61;new JButton();buttonPlay.setBorderPainted(false);buttonPlay.setBounds(90,687,43,45); icon&#61;new ImageIcon(".//play.jpg");buttonPlay.setIcon(icon);add(buttonPlay);buttonPlay.addActionListener(this);buttonPrev&#61;new JButton();buttonPrev.setBorderPainted(false);buttonPrev.setBounds(40,690,38,40);icon&#61;new ImageIcon(".//prev.jpg");buttonPrev.setIcon(icon);add(buttonPrev);buttonPrev.addActionListener(this);buttonNext&#61;new JButton();buttonNext.setBorderPainted(false);buttonNext.setBounds(145,690,38,40);icon&#61;new ImageIcon(".//next.jpg");buttonNext.setIcon(icon);add(buttonNext);buttonNext.addActionListener(this);buttonlist&#61;new JButton();buttonlist.setBorderPainted(false);buttonlist.setBounds(1078,696,30,30);icon&#61;new ImageIcon(".//listplay.jpg");buttonlist.setIcon(icon);add(buttonlist);buttonlist.addActionListener(this);buttonOpen&#61;new JButton();buttonOpen.setBorderPainted(false);buttonOpen.setBounds(1130,700,25,25);icon&#61;new ImageIcon(".//open.jpg");buttonOpen.setIcon(icon);add(buttonOpen);buttonOpen.addActionListener(this);buttonList1&#61;new JButton();buttonList1.setBorderPainted(false);buttonList1.setBounds(1180,696,30,30);icon&#61;new ImageIcon(".//list1.jpg");buttonList1.setIcon(icon);add(buttonList1);buttonList1.addActionListener(this);listPlayFile&#61;new JList(); //创建播放列表 listPlayFile.setBounds(680,125,300,200); //设置播放列表大小 listPlayFile.setOpaque(false);//设置播放列表透明listPlayFile.setVisible(false);listPlayFile.setBackground(new Color(0, 0, 0, 0));//设置播放列表背景颜色listPlayFile.setForeground(Color.blue);//设置播放列表字体颜色Font font&#61;new Font("隶书",Font_PLAIN,20);listPlayFile.setFont(font);add(listPlayFile); //添加播放列表至窗口中listPlayFile.addMouseListener(new MouseAdapter(){public void mouseClicked(MouseEvent e){if(e.getClickCount()&#61;&#61;2){int i &#61; listPlayFile.getSelectedIndex();playPath&#61;Music.get(i).musicPath;playName&#61;Music.get(i).musicName;nTimer.cancel();play_init(playName,playPath);}}});textLyrics1&#61;new JTextPane(); //创建已播放歌词控件textLyrics1.setBounds(1020,100,250,250);textLyrics1.setForeground(Color.red);Font f&#61;new Font("隶书",Font_PLAIN,20);textLyrics1.setFont(f);textLyrics1.setOpaque(false);add(textLyrics1);textLyrics2&#61;new JTextPane(); //创建当前播放歌词控件textLyrics2.setBounds(1010,350,250,36);textLyrics2.setForeground(Color.BLUE);f&#61;new Font("隶书",Font_PLAIN,22);textLyrics2.setFont(f);textLyrics2.setOpaque(false);textLyrics2.setText("请先打开文件!");add(textLyrics2);textLyrics3&#61;new JTextPane(); //创建未播放歌词控件 textLyrics3.setBounds(1020,390,250,258);//设置歌词控件大小textLyrics3.setForeground(Color.red);//歌词控件字体颜色f&#61;new Font("隶书",Font.PLAIN,20);textLyrics3.setFont(f);textLyrics3.setOpaque(false);//歌词控件透明add(textLyrics3); //添加歌词控件至窗口中time&#61;new JTextPane();//定义时间显示窗time.setBounds(200,696,50,25);time.setForeground(Color.BLACK);f&#61;new Font("仿宋",Font_PLAIN,15);time.setFont(f);time.setOpaque(false);add(time);Icon img2&#61;new ImageIcon(".//time.jpg"); //创建图标对象playTime &#61; new JLabel(img2); //创建播放进度条对象playTime.setBounds(265,707,713,6); //设置播放进度条对象大小 add(playTime); //添加播放进度条至窗口中} public void timerFun(){//定时器函数if(nTimer!&#61;null){nTimer.cancel();}//已经有定时器则关闭nTimer &#61; new Timer();//创建定时器nTimer.schedule(new TimerTask(){ //匿名类int n&#61;0; int nPlayTime&#61;0;//设置播放时间public void run() { //定时器playTime.setBounds(265,707, nPlayTime&#43;&#61;6, 6); int t&#61;nPlayTime/6;//秒if(t<&#61;9){time.setText("00:0"&#43;t);}else if(t<60){time.setText("00:"&#43;t);}else if(t<&#61;69){int a&#61;t/60;int b&#61;t%60;time.setText("0"&#43;a&#43;":0"&#43;b);}else{int a&#61;t/60;int b&#61;t%60;time.setText("0"&#43;a&#43;":"&#43;b);}String s&#61;"";String s1&#61;"";if (t&#61;&#61;time0[n]) {textLyrics2.setText(lyc[n]);for(int i&#61;n&#43;1;i&#61;(n-10);i--){s1&#61;lyc[i]&#43;s1;}textLyrics1.setText(s1);}n&#43;&#43;;}if(t&#61;&#61;TIME){nTimer.cancel();switch(schemaflag){case 0:{int m &#61; 0;for(int i&#61;Music.size()-1;i>&#61;0;i--){if(Music.get(i).musicPath.equals(playPath)){m &#61; i;break;}}if(m&#43;1>&#61;Music.size()){playPath&#61;Music.get(0).musicPath;playName&#61;Music.get(0).musicName;play_init(playName,playPath);}else{playPath&#61;Music.get(m&#43;1).musicPath;playName&#61;Music.get(m&#43;1).musicName;play_init(playName,playPath);}}case 1:{play_init(playName,playPath);}case 2:{Random random&#61;new Random();int m&#61;random.nextInt(Music.size());playPath&#61;Music.get(m).musicPath;playName&#61;Music.get(m).musicName;play_init(playName,playPath);}}}}},0,1000);}
}
public class MusicPlay{public static void main(String[] args) { final MyExtendsJFrame frame&#61;new MyExtendsJFrame();//创建窗口frame.buttonmin.addActionListener(new ActionListener(){//点集最小化按钮实现最小化&#64;Override public void actionPerformed(ActionEvent e){frame.setExtendedState(JFrame.ICONIFIED);}});new MobileTool(frame);}
}
.\\表示当前工程下目录
图片的话可以自己用ps抠。