?? mediaplayer.java
字號(hào):
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.media.*;
import java.io.*;
import java.util.*;//為了導(dǎo)入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是個(gè)實(shí)現(xiàn)Controller的接口
private File file,listFile;//利用File類結(jié)合JFileChooser進(jìn)行文件打開操作,后者與listfile有關(guān)
private Container c;
private String title;//標(biāo)題
private FileDialog fd;
private JPanel panel,panelSouth;
private JList list;//播放清單
private JScrollPane scroll;//使播放清單具有滾動(dòng)功能
private ListValues listWriteFile;//用于向文件中讀取對(duì)象
private ObjectInputStream input;//對(duì)象輸入流
private ObjectOutputStream output;//對(duì)象輸出流
private JPopupMenu popupMenu;//鼠標(biāo)右鍵彈出菜單
private JMenuItem del,delAll,reName; //彈出菜單顯示的菜單項(xiàng),包括刪除,全部刪除和重命名
private Vector fileName,dirName,numList;
private String files,dir;
private int index;//曲目指針
private int indexForDel;//標(biāo)志要?jiǎng)h除的列表項(xiàng)目的索引
private ButtonGroup buttonGroup;//控制按鈕組
private JRadioButtonMenuItem[] buttonValues;
private String[] content={"隨機(jī)播放","順序播放","單曲循環(huán)"};
private DialogDemo dialog1;
MediaPlayer()//構(gòu)造函數(shù)
{
super("java音頻播放器");//窗口標(biāo)題
c=getContentPane();
c.setLayout(new BorderLayout());
fileName=new Vector(1);
dirName=new Vector(1);
numList=new Vector(1);//構(gòu)造三個(gè)容器用于支持播放清單
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("點(diǎn)右鍵顯示更多功能");//設(shè)置各個(gè)屬性
list.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
if (e.getClickCount() == 2) //判斷是否雙擊
{
index = list.locationToIndex(e.getPoint());//將鼠標(biāo)坐標(biāo)轉(zhuǎn)化成list中的選項(xiàng)指針
createPlayer2();
}
}
public void mouseReleased(MouseEvent e)
{
checkMenu(e);//判斷是否鼠標(biāo)右鍵,若是,彈出popupmenu,然后進(jìn)行相應(yīng)的菜單操作
}
}
);
scroll=new JScrollPane(list);//用于存放播放列表
readToList.start();//啟動(dòng)線程,加載播放列表,若不啟動(dòng),則程序在第二次啟動(dòng)時(shí)播放列表中沒有任何東西
try
{
Thread.sleep(10);//每10秒鐘檢測(cè)一下是否應(yīng)該停止播放
}
catch(InterruptedException e)
{
e.printStackTrace();
}
bar=new JMenuBar();
bar.setBackground(Color.green);
setJMenuBar(bar);//此兩行創(chuàng)建菜單欄并放到此窗口程序
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("軟件簡(jiǎn)介");
aboutMenu.add(infor);
infor.addActionListener(this);
about=new JMenuItem("關(guān)于作者");
about.addActionListener(this);
aboutMenu.add(about);
//菜單欄設(shè)置完畢
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("刪除");//鼠標(biāo)右鍵彈出菜單對(duì)象實(shí)例化
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);//設(shè)定窗口關(guān)閉方式
this.setLocation(400,250);//設(shè)定窗口出現(xiàn)的位置
setSize(350,330);
this.setResizable(false);//設(shè)置播放器不能隨便調(diào)大小
this.setVisible(true);//此句不可少,否則窗口會(huì)不顯示
}
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,"此簡(jiǎn)易播放器由計(jì)科053\n"
+"郝燕青\n "+" 完成 ",
"參與者",
JOptionPane.INFORMATION_MESSAGE);
}
if(e.getSource()==del)//刪除后后面的選項(xiàng)要跟著前移
{
fileName.removeElementAt(indexForDel);
dirName.removeElementAt(indexForDel);
numList.removeAllElements();//從三個(gè)容器里面移除此項(xiàng)
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開始)是目前選中的歌曲項(xiàng)
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()//注冊(cè)窗口事件
{
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,"錯(cuò)誤的路徑",
"出錯(cuò)了",JOptionPane.ERROR_MESSAGE);
String[] sFiles=file.list();
for(int i=0;i<sFiles.length;i++)//此處若是調(diào)用addList()則效率降低
{
fileName.addElement(sFiles[i]);
numList.addElement((numList.size()+1)+"."+sFiles[i]);
dirName.addElement(file.getAbsolutePath()+"\\"+sFiles[i]);
}
list.setListData(numList);
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -