📄 mediaplayer.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.media.*;
import java.io.*;
import java.util.*;//为了导入Vector
public class MediaPlayer extends JFrame implements ActionListener,Runnable
{
private JMenuBar bar;//菜单条
private JMenu fileMenu,choiceMenu,aboutMenu;
private JMenuItem openItem,openDirItem,closeItem,about,infor;
private Player player;//Play是个实现Controller的接口
private File file,listFile;//利用File类结合JFileChooser进行文件打开操作,后者与listfile有关
private Container c;
private String title;//标题
private FileDialog fd;
private JPanel panel,panelSouth;
private JList list;//播放清单
private JScrollPane scroll;//使播放清单具有滚动功能
private ListValues listWriteFile;//用于向文件中读取对象
private ObjectInputStream input;//对象输入流
private ObjectOutputStream output;//对象输出流
private JPopupMenu popupMenu;//鼠标右键弹出菜单
private JMenuItem del,delAll,reName; //弹出菜单显示的菜单项,包括删除,全部删除和重命名
private Vector fileName,dirName,numList;
private String files,dir;
private int index;//曲目指针
private int indexForDel;//标志要删除的列表项目的索引
private ButtonGroup buttonGroup;//控制按钮组
private JRadioButtonMenuItem[] buttonValues;
private String[] content={"随机播放","顺序播放","单曲循环"};
private DialogDemo dialog1;
MediaPlayer()//构造函数
{
super("java音频播放器");//窗口标题
c=getContentPane();
c.setLayout(new BorderLayout());
fileName=new Vector(1);
dirName=new Vector(1);
numList=new Vector(1);//构造三个容器用于支持播放清单
listFile=new File("listfile");//直接存于此目录
Thread readToList=new Thread(this);
list=new JList();
list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
list.setSelectionForeground(new Color(0,150,150));
list.setVisibleRowCount(10);
list.setFixedCellHeight(12);
list.setFixedCellWidth(250);
list.setFont(new Font("Serif",Font.PLAIN,12));
list.setBackground(new Color(227,136,240));
list.setToolTipText("点右键显示更多功能");//设置各个属性
list.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
if (e.getClickCount() == 2) //判断是否双击
{
index = list.locationToIndex(e.getPoint());//将鼠标坐标转化成list中的选项指针
createPlayer2();
}
}
public void mouseReleased(MouseEvent e)
{
checkMenu(e);//判断是否鼠标右键,若是,弹出popupmenu,然后进行相应的菜单操作
}
}
);
scroll=new JScrollPane(list);//用于存放播放列表
readToList.start();//启动线程,加载播放列表,若不启动,则程序在第二次启动时播放列表中没有任何东西
try
{
Thread.sleep(10);//每10秒钟检测一下是否应该停止播放
}
catch(InterruptedException e)
{
e.printStackTrace();
}
bar=new JMenuBar();
bar.setBackground(Color.green);
setJMenuBar(bar);//此两行创建菜单栏并放到此窗口程序
fileMenu=new JMenu("文件");
bar.add(fileMenu);
choiceMenu=new JMenu("控制");
bar.add(choiceMenu);
aboutMenu=new JMenu("帮助");
bar.add(aboutMenu);
openItem =new JMenuItem("打开文件");
openDirItem =new JMenuItem("打开目录");
closeItem =new JMenuItem("退出程序");
openItem.addActionListener(this);
openDirItem.addActionListener(this);
closeItem.addActionListener(this);
fileMenu.add(openItem);
fileMenu.add(openDirItem);
fileMenu.add(closeItem);
buttonGroup=new ButtonGroup();
buttonValues=new JRadioButtonMenuItem[3];
for(int bt=0;bt<3;bt++)
{
buttonValues[bt]=new JRadioButtonMenuItem(content[bt]);
buttonGroup.add(buttonValues[bt]);
choiceMenu.add(buttonValues[bt]);
}
buttonValues[0].setSelected(true);
choiceMenu.addSeparator();
infor=new JMenuItem("软件简介");
aboutMenu.add(infor);
infor.addActionListener(this);
about=new JMenuItem("关于作者");
about.addActionListener(this);
aboutMenu.add(about);
//菜单栏设置完毕
panel=new JPanel();
panel.setLayout(new BorderLayout());
panel.setBackground(new Color(227,136,240));
c.add(panel,BorderLayout.CENTER);
panelSouth=new JPanel();
panelSouth.setLayout(new BorderLayout());
c.add(panelSouth,BorderLayout.SOUTH);
popupMenu=new JPopupMenu();
del =new JMenuItem("删除");//鼠标右键弹出菜单对象实例化
popupMenu.add(del);
del.addActionListener(this);
popupMenu.setBackground(Color.green);
delAll =new JMenuItem("全部删除");
popupMenu.add(delAll);
delAll.addActionListener(this);
reName =new JMenuItem("重命名");
popupMenu.add(reName);
reName.addActionListener(this);
scroll=new JScrollPane(list);//用于存放播放列表
panelSouth.add(scroll,BorderLayout.CENTER);
dialog1=new DialogDemo(MediaPlayer.this,"软件说明");
this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);//设定窗口关闭方式
this.setLocation(400,250);//设定窗口出现的位置
setSize(350,330);
this.setResizable(false);//设置播放器不能随便调大小
this.setVisible(true);//此句不可少,否则窗口会不显示
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==openItem)
{
openFile();
}
if(e.getSource()==openDirItem)//打开目录
{
openDir();
}
if(e.getSource()==closeItem)//退出播放器
{
exity_n();
}
if(e.getSource()==about)
{
JOptionPane.showMessageDialog(this,"此简易播放器由计科053\n"
+"郝燕青\n "+" 完成 ",
"参与者",
JOptionPane.INFORMATION_MESSAGE);
}
if(e.getSource()==del)//删除后后面的选项要跟着前移
{
fileName.removeElementAt(indexForDel);
dirName.removeElementAt(indexForDel);
numList.removeAllElements();//从三个容器里面移除此项
Enumeration enumFile=fileName.elements();
while(enumFile.hasMoreElements())
{
numList.addElement((numList.size()+1)+"."+enumFile.nextElement());
//numList添加元素,显示播放列表中
}
list.setListData(numList);
}
if(e.getSource()==delAll)//全部删除
{
fileName.removeAllElements();
dirName.removeAllElements();
numList.removeAllElements();
list.setListData(numList);
}
if(e.getSource()==reName)//重命名
{
String name;
try
{
name=reNames();
fileName.setElementAt(name,indexForDel);//indexForDel(从0开始)是目前选中的歌曲项
numList.setElementAt((indexForDel+1)+"."+name,indexForDel);
}
catch(Exception e2)
{
}
}
if(e.getSource()==infor)
{
dialog1.setVisible(true);
}
}
public static void main(String[] args)
{
final MediaPlayer mp=new MediaPlayer();
mp.addWindowListener(new WindowAdapter()//注册窗口事件
{
public void windowClosing(WindowEvent e)
{
mp.exity_n();
}
}
);
}
private void openFile()
{
fd = new FileDialog(this);
fd.setVisible(true);
if (fd.getFile() != null)
{
title = fd.getDirectory() + fd.getFile();
files=fd.getFile();
file=new File(title);
addList(fd.getFile());
}
}
private void openDir()//本来用的是FileDialog,但是它打开的是文件而不是目录,所以就用了JFileChooser
{
JFileChooser fileChooser=new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int result=fileChooser.showOpenDialog(this);
if(result==JFileChooser.CANCEL_OPTION)
return;
file=fileChooser.getSelectedFile();
if(file==null||file.getName().equals(""))
JOptionPane.showMessageDialog(this,"错误的路径",
"出错了",JOptionPane.ERROR_MESSAGE);
String[] sFiles=file.list();
for(int i=0;i<sFiles.length;i++)//此处若是调用addList()则效率降低
{
fileName.addElement(sFiles[i]);
numList.addElement((numList.size()+1)+"."+sFiles[i]);
dirName.addElement(file.getAbsolutePath()+"\\"+sFiles[i]);
}
list.setListData(numList);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -