?? mediaplayer.java
字號:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.media.*;
import java.io.*;
import java.util.*;//為了導入Vector
import com.sun.java.swing.plaf.windows.*;
import java.io.Serializable;
public class MediaPlayer extends JFrame implements ActionListener,Runnable
{
class ListValues implements Serializable
{
private String fileName;
private String dirFileName;
ListValues()
{
setFileName("歌曲名字");
setDirFileName("E:\\歌曲名字");
}
ListValues(String fileNameC,String dirFileNameC)
{
setFileName(fileNameC);
setDirFileName(dirFileNameC);
}
void setFileName(String fileNameC)
{
fileName=fileNameC;
}
void setDirFileName(String dirFileNameC)
{
dirFileName=dirFileNameC;
}
String getFileName()
{
return fileName;
}
String getDirName()
{
return dirFileName;
}
}
class DialogDemo extends JDialog
{
JTextArea field;
Container c;
String sValue= "\n本軟件基于java media framework構建,同時我們"
+"\n正處于基礎java學習階段,所以功能還不是非常完"
+"\n善,難登大雅之堂。"
+"\n\n\t功能簡介:"
+"\n本軟件的播放清單文件保存在同目錄下的"+"\""+"list.ini"
+"\""+"文件"+"\n下,如果系統不存在此文件則第一次打開的時候軟件"
+"\n會自動建立。本軟件能打開單個音樂文件或整個音樂"
+"\n目錄,清單支持右鍵操作,當在列表中讀取到不支持的"
+"\n文件時,此軟件會自動把那個文件從清單清除,并另"
+"\n外播放一首歌。不過此軟件有個不足之處,就是當你更"
+"\n改了清單的時候,要正常退出,即先關閉播放器,然后"
+"\n再關閉DOS窗口,因為我是在關閉播放器的時候保存清單"
+"\n文件的。不過你運行我編譯出來的jar文件就沒有此問題"
+"\n如果你使用中遇到任何問題,請通知我們,謝謝你的支持";
DialogDemo(Frame frame,String title)
{
super(frame,title);
field=new JTextArea();
field.setText(sValue);
field.setEditable(false);
c=getContentPane();
c.setLayout(new BorderLayout());
c.add(field,BorderLayout.CENTER);//默認為BorderLayout布局
setLocation(80,250);
setSize(300,350);
setResizable(false);
// setVisible(true);
}
}
private JMenuBar bar;//菜單條
private JMenu fileMenu,choiceMenu,aboutMenu;
private JMenuItem openItem,openDirItem,closeItem,about,infor;
private JCheckBoxMenuItem onTop;
private boolean top=false,loop;//設定窗口是否在最前面
private Player player;//Play是個實現Controller的接口
private File file,listFile;//利用File類結合JFileChooser進行文件打開操作,后則與list.ini有關
private Container c;
//private UIManager.LookAndFeelInfo[] look;
private String title,listIniAddress;//標題
private FileDialog fd;
private JPanel panel,panelSouth;
private Icon icon; //開始進入的時候要顯示的圖標,它為抽象類,不能自己創建
private JLabel label,listB;//用來顯示圖標
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 Properties prop;//獲得系統屬性
private int indexForDel;//標志要刪除的列表項目的索引
private ButtonGroup buttonGroup;//控制按鈕組
private JRadioButtonMenuItem[] buttonValues;
private String[] content={"隨機播放","順序播放","單曲循環"};
private DialogDemo dialog1;
//private JDialogTest dialog2;//用于顯示播放清單
MediaPlayer()//構造函數
{
super("音樂播放器");//窗口標題
c=getContentPane();
c.setLayout(new BorderLayout());
//c.setBackground(new Color(40,40,95));
fileName=new Vector(1);
dirName=new Vector(1);
numList=new Vector(1);
//vectorToString=new String[];
prop=new Properties(System.getProperties());
//listIniAddress=prop.getProperty("user.dir")+"\\list.ini";
//listFile=new File(listIniAddress);
listFile=new File("list.ini");//直接存于此目錄
Thread readToList=new Thread(this);//注意編線程程序的時候要注意運行的時候含有的變量億定義或者初始化,
//這就要求線程要等上述所說的情況下再運行,否則很容易發生錯誤或則異常
list=new JList();
list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
list.setSelectionForeground(new Color(0,150,150));
list.setVisibleRowCount(5);
list.setFixedCellHeight(12);
list.setFixedCellWidth(250);
list.setFont(new Font("Serif",Font.PLAIN,12));
list.setBackground(new Color(40,40,95));
list.setForeground(new Color(0,128,255));
//list.setOpaque(false);
list.setToolTipText("點右鍵顯示更多功能");
list.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
if (e.getClickCount() == 2)
{
index = list.locationToIndex(e.getPoint());
createPlayer2();
//System.out.println("Double clicked on Item " + index);,此是測試的時候加的
}
}
public void mouseReleased(MouseEvent e)
{
checkMenu(e);//與上面的一樣
}
} );
//listB=new JLabel(new ImageIcon("qingdan.gif"),SwingConstants.CENTER);
scroll=new JScrollPane(list);//用于存放播放列表
//dialog2=new JDialogTest(MediaPlayer.this,"播放清單",scroll);
//dialog2.setVisible(true);
readToList.start();
try
{
Thread.sleep(10);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
bar=new JMenuBar();
setJMenuBar(bar);//此兩行創建菜單欄并放到此窗口程序
//bar.setBackground(new Color(48,91,183));
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);
onTop=new JCheckBoxMenuItem("播放時位于最前面",top);
choiceMenu.add(onTop);
onTop.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
if(onTop.isSelected())
top=true;
else top=false;
setAlwaysOnTop(top);
}
} );
choiceMenu.addSeparator();
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();
/*loopItem=new JCheckBoxMenuItem("是否循環");
choiceMenu.add(loopItem);
loopItem.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
loop=!loop;
}
}
);*/
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());
c.add(panel,BorderLayout.CENTER);
panelSouth=new JPanel();
panelSouth.setLayout(new BorderLayout());
c.add(panelSouth,BorderLayout.SOUTH);
icon=new ImageIcon("icon\\Player.jpg");
label=new JLabel(icon);
panel.add(label);
popupMenu=new JPopupMenu();
del =new JMenuItem("刪除");//鼠標右鍵彈出菜單對象實例化
popupMenu.add(del);
del.addActionListener(this);
delAll =new JMenuItem("全部刪除");
popupMenu.add(delAll);
delAll.addActionListener(this);
reName =new JMenuItem("重命名");
popupMenu.add(reName);
reName.addActionListener(this);
scroll=new JScrollPane(list);//用于存放播放列表
listB=new JLabel(new ImageIcon("icon\\qingdan.gif"),SwingConstants.CENTER);
panelSouth.add(listB,BorderLayout.NORTH);
panelSouth.add(scroll,BorderLayout.CENTER);
dialog1=new DialogDemo(MediaPlayer.this,"軟件說明");
Toolkit kit =Toolkit.getDefaultToolkit();
Image image=kit.createImage("qingdan.jpg");
setIconImage(image);
this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);//設定窗口關閉方式
//this.setTitle("d");編譯通過,說明可以再次設定標題
this.setLocation(400,250);//設定窗口出現的位置
//this.setSize(350,120);//窗口大小
setSize(350,145);
this.setResizable(true);
this.setVisible(true);//此句不可少,否則窗口會不顯示
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==openItem)
{
//System.out.println("d");測試用
openFile();
//createPlayer();
//setTitle(title);
}
if(e.getSource()==openDirItem)
{
openDir();
}
if(e.getSource()==closeItem)
{
exity_n();
//System.exit(0);
}
if(e.getSource()==about)
{
JOptionPane.showMessageDialog(this,"此簡易播放器由計本\n"
+"梅月平(206) \n "+" 完成 ",
"參與者",
JOptionPane.INFORMATION_MESSAGE);
}
if(e.getSource()==del)
{
//index
//delPaintList(index);
fileName.removeElementAt(indexForDel);
dirName.removeElementAt(indexForDel);
numList.removeAllElements();
Enumeration enumFile=fileName.elements();
while(enumFile.hasMoreElements())
{
numList.addElement((numList.size()+1)+"."+enumFile.nextElement());
}
//list.setListData(fileName);
list.setListData(numList);
if(index<indexForDel)
list.setSelectedValue(numList.elementAt(index),true);
else
{
if(index==indexForDel);
else
if(index!=0)
list.setSelectedValue(numList.elementAt(index-1),true);
}
//list.setSelectedIndex(index);
}
if(e.getSource()==delAll)
{
fileName.removeAllElements();
dirName.removeAllElements();
numList.removeAllElements();
list.setListData(numList);
}
if(e.getSource()==reName)
{
String name;//=JOptionPane.showInputDialog(this,"請輸入新的名字");
try
{
name=reNames();
fileName.setElementAt(name,indexForDel);
numList.setElementAt((indexForDel+1)+"."+name,indexForDel);
}
catch(ReName e2)
{
}
}
if(e.getSource()==infor)
{
dialog1.setVisible(true);
}
}
public static void main()
{
final MediaPlayer mp=new MediaPlayer();
//mp.setIconImage(new ImageIcon("icon\\mPlayer.jpg").getImage());//改變默認圖標
mp.addWindowListener(new WindowAdapter()//注冊窗口事件
{
public void windowClosing(WindowEvent e)
{
// this.close();
mp.exity_n();
return;
}
} );
System.out.println("注意:更新文件列表后,請先正常關閉播放器"
+"\n然后再關閉此DOS窗口,否則導致播放列表不能保存!!");
}
private void openFile()//為了界面原因,此代碼重寫,估計兼容性不好了
{
/*JFileChooser fileChooser=new JFileChooser();//文件選擇器
* fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);//可以選擇文件不能目錄
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -