?? mediaplayer.java
字號:
/*
* MediaPlayer.java
*
* Created on 2004年1月23日, 上午12:47
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package media;
/**
*
* @author zhongweijian
*/
/*
* Main.java
*
* Created on 2004年1月23日, 上午12:38
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.media.*;
import java.io.*;
import java.util.*;//為了導入Vector
import com.sun.media.ui.*;
import javax.media.protocol.*;
import javax.media.protocol.DataSource;
//import com.sun.java.swing.plaf.windows.*;
public class MediaPlayer extends JFrame implements ActionListener,Runnable
{
private JList list;//播放清單
private JScrollPane scroll;//使播放清單具有滾動功能
private ListValues listWriteFile;//用于向文件中讀取對象
private ObjectInputStream input;//對象輸入流
private ObjectOutputStream output;//對象輸出流
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 JPopupMenu popupMenu;//鼠標右鍵彈出菜單
private JMenuItem del,delAll,reName; //彈出菜單顯示的菜單項,包括刪除,全部刪除和重命名
private DialogDemo dialog1;
private JMenuBar bar;//菜單條
private JMenu fileMenu,choiceMenu,voiceMenu,helpMenu;//the name of bar
private JMenuItem openItem,openDirItem,closeItem,about,info,voicehigh,voicelow;//the name of menu
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 String title,listIniAddress;//標題
private FileDialog fd;
private JPanel panel,panelSouth;
private Icon icon;
private JLabel label,listB;//用來顯示圖標
MediaPlayer()//構造函數
{
super("java媒體播放器");//窗口標題
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); //構造三個容器用于支持播放清單
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(10);
list.setFixedCellHeight(12);
list.setFixedCellWidth(250);
list.setFont(new Font("Serif",Font.PLAIN,14));
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());//將鼠標坐標轉化成list中的選項指針
createPlayer2();
System.out.println("Double clicked on Item " + (index+1));
}
}
public void mouseReleased(MouseEvent e)
{
checkMenu(e);
}
}
);
scroll=new JScrollPane(list);//用于存放播放列表
readToList.start();//啟動先程,加載播放列表
try
{
Thread.sleep(100);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
/* try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e)
{
e.printStackTrace();
}
*/
bar=new JMenuBar();
setJMenuBar(bar);//此兩行創建菜單欄并放到此窗口程序
bar.setBackground(new Color(10,160,173));
fileMenu=new JMenu("文件");
bar.add(fileMenu);
choiceMenu=new JMenu("控制");
bar.add(choiceMenu);
voiceMenu=new JMenu("聲音");
bar.add(voiceMenu);
helpMenu=new JMenu("幫助");
bar.add(helpMenu);
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); //設置ontop菜單項
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();
voicehigh=new JMenuItem("升高");
voiceMenu.add(voicehigh);
voicehigh.addActionListener(this);
voicelow=new JMenuItem("降低");
voicelow.addActionListener(this);
voiceMenu.add(voicelow); //菜單欄設置完畢
about=new JMenuItem("關于播放器");
helpMenu.add(about);
about.addActionListener(this);
info=new JMenuItem("播放器使用指南");
helpMenu.add(info);
info.addActionListener(this);
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("紫花.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("雪景2.jpg"),SwingConstants.CENTER);
panelSouth.add(listB,BorderLayout.NORTH);
panelSouth.add(scroll,BorderLayout.CENTER);
dialog1=new DialogDemo(this,"說明");
this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);//設定窗口關閉方式
this.setLocation(400,250);//設定窗口出現的位置
//this.setSize(350,320);//窗口大小
setSize(450,430);
this.setResizable(false);//設置播放器不能隨便調大小
this.setVisible(true);//設置窗口顯示
TypeWord type=new TypeWord();//動畫顯示模塊
type.isStandalone=true;
JFrame frame;
frame=new JFrame() //the constructor of frame
{
protected void processWindowEvent(WindowEvent e)
{
super.processWindowEvent(e);
if(e.getID()==WindowEvent.WINDOW_CLOSING)
{
//System.exit(0);
// type.stop();
// frame.setVisible(false); //this three lines do not work
}
}
public synchronized void setTitle(String title)
{
super.setTitle(title);
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
}
};
frame.add(type,BorderLayout.CENTER);
type.init(); //線程開始即動畫初始化
type.start(); //線程開始動畫顯示
Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((d.width-frame.getSize().width)/2-112,(d.height-frame.getSize().height)/2-90);
frame.setVisible(true);
frame.setBackground(Color.GREEN);
frame.setSize(450,100);
//c.add(frame,BorderLayout.NORTH);這句加不進去,不知怎么把動畫加到播放器中
//add(frame,BorderLayout.NORTH);
// desktop.add(frame);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==openItem)//getSource()判斷發生時間的組鍵
{
openFile();
}
if(e.getSource()==openDirItem)//打開目錄
{
openDir();
}
if(e.getSource()==closeItem)//退出播放器
{
exity_n();
//System.exit(0);
}
if(e.getSource()==about)
{
JOptionPane.showMessageDialog(this,"This is a simple mediaplayer! Thanks!","content",
JOptionPane.INFORMATION_MESSAGE);
//System.out.println("shengying:"+getVolumeLevel());
}
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(index<indexForDel)
list.setSelectedValue(numList.elementAt(index),true);
else
{
if(index==indexForDel);
else
if(index!=0)
list.setSelectedValue(numList.elementAt(index-1),true);
}
}
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);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -