⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 listpanel.java

📁 自己制作一个音频文件播放器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 * 显示列表面板
 */
package myPackage;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.media.*;
import javax.swing.*;

/**
 * @author guanchun
 *
 */
public class ListPanel extends JPanel implements ControllerListener{
	public ListPanel(MainPanel pane)
	{
		panel = pane;
		setLayout(new BorderLayout(2,2));
		northpanel = new JPanel();
		menupanel = new JPanel();
		leftpanel = new JPanel();
		rightpanel = new JPanel();
		companel = new JPanel();
		add(northpanel,BorderLayout.NORTH);
		add(leftpanel,BorderLayout.WEST);
		add(rightpanel,BorderLayout.CENTER);
		northpanel.setPreferredSize(new Dimension(300,50));
		leftpanel.setPreferredSize(new Dimension(100,300));
		
		menubar = new JMenuBar();
		filemenu = new JMenu("文件");
		listmenu = new JMenu("列表");
		modemenu = new JMenu("模式");
		menubar.add(filemenu);
		menubar.add(listmenu);
		menubar.add(modemenu);
		
		addfileItem = new JMenuItem("添加文件");
		addFileItem = new JMenuItem("添加文件夹");
		delfileItem = new JMenuItem("删除文件");
		filemenu.add(addfileItem);
		filemenu.add(addFileItem);
		filemenu.add(delfileItem);
		
		addlistItem = new JMenuItem("新建列表");
		dellistItem = new JMenuItem("删除列表");
		listmenu.add(addlistItem);
		listmenu.add(dellistItem);
		
		oneloopItem = new JMenuItem("单曲循环");
		orderlyItem = new JMenuItem("顺序播放");
		loopItem = new JMenuItem("循环播放");
		modemenu.add(oneloopItem);
		modemenu.add(orderlyItem);
		modemenu.add(loopItem);
		
		northpanel.setLayout(new BorderLayout());
		northpanel.add(menupanel,BorderLayout.SOUTH);
		menupanel.setPreferredSize(new Dimension(300,25));
		menupanel.setLayout(new BorderLayout());
		menupanel.add(menubar,BorderLayout.WEST);
		
		modelabel = new JLabel("顺序播放");
		menupanel.add(modelabel,BorderLayout.EAST);
		modelabel.setPreferredSize(new Dimension(60,25));
		modelabel.setFont(new Font("宋体",Font.PLAIN,12));
		
		northpanel.add(companel,BorderLayout.NORTH);
		companel.setPreferredSize(new Dimension(300,25));
		companel.setLayout(new BorderLayout());
		companel.setBackground(Color.lightGray);
		
		playlistdata = new PlayListData();
		leftlist = new JList();
		leftpanel.setLayout(new BorderLayout());
		leftpanel.add(new JScrollPane(leftlist),BorderLayout.CENTER);
		leftlist.setFont(new Font("宋体",Font.PLAIN,12));
		
		rightlist = new JList();
		rightpanel.setLayout(new BorderLayout());
		rightpanel.add(new JScrollPane(rightlist),BorderLayout.CENTER);
        rightlist.setFont(new Font("宋体",Font.PLAIN,12));
		setActionListener ();
		
	}
	
	//获得播放路径
	public String getPlaypath()
	{
		playindex = leftlist.getSelectedIndex();
		if(playindex!=-1)
		{
			if(nextORfront == null)
			{
		    	songindex = rightlist.getSelectedIndex();
		    	if(songindex!=-1)
		    	{
		     		songlistdata = playlistdata.getSongindex(playindex);
			    	playpath = songlistdata.getsongpath(songindex);
			    }
			}
			//下一曲
			else if(nextORfront.equals("next"))
			{
				songindex = rightlist.getSelectedIndex();
				if(songindex != -1)
				{
					songlistdata = playlistdata.getSongindex(playindex);
			      	if(songindex == songlistdata.getlength()-1)//列表中的最后一曲
			    	{
				    	if(playmode == 3) //循环播放
				    	{
					    	songindex = 0;
				    		rightlist.setSelectedIndex(songindex);//跳到第一曲
			    		}
			    		playpath = songlistdata.getsongpath(songindex);
			    	}
		    		else 
			    	{
		    			if(playmode == 1) {}
		    			else
				        	songindex++;
			    		rightlist.setSelectedIndex(songindex);
			    		playpath = songlistdata.getsongpath(songindex);
			    	}
				}
			}
			//上一曲
			else if(nextORfront.equals("front"))
			{
				songindex = rightlist.getSelectedIndex();
				if(songindex != -1)
				{
					songlistdata = playlistdata.getSongindex(playindex);
			      	if(songindex == 0)//列表中的第一曲
			    	{
				    	if(playmode == 3) //循环播放
				    	{
					    	songindex = songlistdata.getlength()-1;
				    		rightlist.setSelectedIndex(songindex);//跳到最后一曲
			    		}
			    		playpath = songlistdata.getsongpath(songindex);
			    	}
		    		else 
			    	{
		    			if(playmode == 1) {}
		    			else
				        	songindex--;
			    		rightlist.setSelectedIndex(songindex);
			    		playpath = songlistdata.getsongpath(songindex);
			    	}
				}
			}
		}
		else playpath = null;
		return playpath;
	}
	
	//播放
	public void startplay()
	{
		if(aplayer != null)
		{
			aplayer.start();
		}
		else 
		{
			if(getPlaypath() != null)
			{
				nextORfront = null;
	    		createplayer(getPlaypath());
	        }
			else { }
		}
	}
	
	//暂停
	public void puaseplay()
	{
		if(aplayer != null)
		{
			//state = 0;
			aplayer.stop();
		}
		else {}
	}
	
	//停止播放
	public void stopplay()
	{
		if(aplayer != null)
		{
			aplayer.close();
			aplayer = null;
		}
	}
	
	//下一曲
	public void nextplay()
	{
		if(aplayer != null)
		{
			nextORfront = "next";
			aplayer.close();
			aplayer = null;
			startplay();
		}
	}
	
	//上一曲
	public void frontplay()
	{
		if(aplayer != null)
		{
			nextORfront = "front";
			aplayer.close();
			aplayer = null;
			startplay();
		}
	}
	
	//保存列表数据
	public void saveList()
	{
		File file = new File("save/");
		File[] files= file.listFiles();
		for(int   i   =   0;   i   <   files.length;   i++) 
	    { 
	        files[i].delete();
        } 
		
		if(playlistdata.getLength() == 0) return;
        //创建save文件夹
		try
		{
			new File("save/").mkdir();
		}
		catch(SecurityException e)
		{
			JOptionPane.showMessageDialog(ListPanel.this, "创建文件夹失败!", "错误", JOptionPane.ERROR_MESSAGE);
			return;
		}
        
		Properties playlist = new Properties();
		if( playlistdata.getLength() == 0) return;
		//保存列表数目
		playlist.put("playlistnum",String.valueOf(playlistdata.getLength()));
		//保存上次退出时播放列表计数器
		playlist.put("playindex",String.valueOf(playindex));
		//保存上次退出时歌曲列表计数器
		playlist.put("songindex",String.valueOf(songindex));
		int i = 0;
		//保存列表名
		while( i < playlistdata.getLength())
		{
			playlist.put("listname"+i,playlistdata.getListname(i));
			i++;
		}
		
		try{
			FileOutputStream out = new FileOutputStream("save/playlist.properties");
			playlist.store(out, "save.properties");
			out.close();
		}catch(IOException ex){
			JOptionPane.showMessageDialog(ListPanel.this, "文件输出错误!", "错误", JOptionPane.ERROR_MESSAGE);
			return;
		}
		
		//保存歌曲列表
		for(i = 0; i < playlistdata.getLength(); i++)
		{
			Properties songlist = new Properties();
			//保存第i个播放列表的歌曲数
			int songnum = playlistdata.getSongindex(i).getlength();
			songlist.put("songnum", String.valueOf(songnum));
			for(int j = 0; j < songnum; j++)
			{
				songlist.put("songname"+j,playlistdata.getSongindex(i).getsongname(j));
				songlist.put("songpath"+j,playlistdata.getSongindex(i).getsongpath(j));
			}
			try{
				FileOutputStream out = new FileOutputStream("save/songlist"+i+".properties");
				songlist.store(out, "save/songlist"+i+".properties");
				out.close();
				}catch(IOException ex){
					JOptionPane.showMessageDialog(ListPanel.this, "文件输出错误!", "错误", JOptionPane.ERROR_MESSAGE);
					return;
				}
		}
    }
	
	//程序开始运行时获得以前保存的列表数据
	public void getData()
	{
		//读入播放列表数据
		Properties setplaylist = new Properties();
		try
		{
			FileInputStream in = new FileInputStream("save/playlist.properties");
			setplaylist.load(in);
			in.close();
		}catch(IOException ex){
			//JOptionPane.showMessageDialog(ListPanel.this, "文件读入错误!", "错误", JOptionPane.ERROR_MESSAGE);
			return;
		}
		int lists = Integer.parseInt(setplaylist.getProperty("playlistnum"));
		for(int i = 0; i<lists; i++)
		{
			playlistdata.setListname(setplaylist.getProperty("listname"+i));
		}
		//读入歌曲列表数据
		for(int i = 0; i < lists; i++)
		{
			Properties setsonglist = new Properties();
			try
			{
				FileInputStream in = new FileInputStream("save/songlist"+i+".properties");
				setsonglist.load(in);
				in.close();
			}catch(IOException ex){
				JOptionPane.showMessageDialog(ListPanel.this, "文件读入错误!", "错误", JOptionPane.ERROR_MESSAGE);
				return;
			}
			int songnum = Integer.parseInt(setsonglist.getProperty("songnum"));
			songfiles = songnum;
			for(int j = 0; j < songnum; j++)
			{
				songlistdata = playlistdata.getSongindex(i);
    			songlistdata.setsongname(setsonglist.getProperty("songname"+j), 
    					setsonglist.getProperty("songpath"+j));
			}
		}
		playindex = Integer.parseInt(setplaylist.getProperty("playindex"));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -