myeclipse编写的视频播放器源代码.docx
- 文档编号:683868
- 上传时间:2023-04-29
- 格式:DOCX
- 页数:20
- 大小:21.31KB
myeclipse编写的视频播放器源代码.docx
《myeclipse编写的视频播放器源代码.docx》由会员分享,可在线阅读,更多相关《myeclipse编写的视频播放器源代码.docx(20页珍藏版)》请在冰点文库上搜索。
myeclipse编写的视频播放器源代码
第一步:
我用的是MyEclipse5.5。
所以,先建立一个class文件,名字:
MediaPlayer.java
完整代码如下:
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
importjavax.media.*;
importjava.io.*;
importjava.util.*;//为了导入Vector
publicclassMediaPlayerextendsJFrameimplementsActionListener,Runnable
{
privateJMenuBarbar;//菜单条
privateJMenufileMenu,choiceMenu,aboutMenu;
privateJMenuItemopenItem,openDirItem,closeItem,about,infor;
privateJCheckBoxMenuItemonTop;
privatebooleantop=false,loop;//设定窗口是否在最前面
privatePlayerplayer;//Play是个实现Controller的接口
privateFilefile,listFile;//利用File类结合JFileChooser进行文件打开操作,后则与list.ini有关
privateContainerc;
//privateUIManager.LookAndFeelInfo[]look;
privateStringtitle,listIniAddress;//标题
privateFileDialogfd;
privateJPanelpanel,panelSouth;
privateIconicon;//开始进入的时候要显示的图标,它为抽象类,不能自己创建
privateJLabel
//listB=newJLabel(newImageIcon("qingdan.gif"),SwingConstants.CENTER);
scroll=newJScrollPane(list);//用于存放播放列表
//dialog2=newJDialogTest(MediaPlayer.this,"播放清单",scroll);
//dialog2.setVisible(true);
readToList.start();//启动先程,加载播放列表
try
{
Thread.sleep(10);
}
catch(InterruptedExceptione)
{
e.printStackTrace();
}
/*look=UIManager.getInstalledLookAndFeels();
try
{
UIManager.setLookAndFeel(look[2].getClassName());
SwingUtilities.updateComponentTreeUI(this);
}
catch(Exceptione)
{
e.printStackTrace();
}*///与下面的代码实现相同的功能,但执行速度要慢,原因:
明显转了个大弯
/*try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exceptione)
{
e.printStackTrace();
}*///此段代码使执行速度大大降低
bar=newJMenuBar();
setJMenuBar(bar);//此两行创建菜单栏并放到此窗口程序
//bar.setBackground(newColor(48,91,183));
fileMenu=newJMenu("文件");
bar.add(fileMenu);
choiceMenu=newJMenu("控制");
bar.add(choiceMenu);
aboutMenu=newJMenu("帮助");
bar.add(aboutMenu);
openItem =newJMenuItem("打开文件");
openDirItem=newJMenuItem("打开目录");
closeItem =newJMenuItem("退出程序");
openItem.addActionListener(this);
openDirItem.addActionListener(this);
closeItem.addActionListener(this);
fileMenu.add(openItem);
fileMenu.add(openDirItem);
fileMenu.add(closeItem);
onTop=newJCheckBoxMenuItem("播放时位于最前面",top);
choiceMenu.add(onTop);
onTop.addItemListener(newItemListener()
{
publicvoiditemStateChanged(ItemEvente)
{
if(onTop.isSelected())
top=true;
elsetop=false;
setAlwaysOnTop(top);
}
}
);
choiceMenu.addSeparator();//加分割符号
buttonGroup=newButtonGroup();
buttonValues=newJRadioButtonMenuItem[3];
for(intbt=0;bt<3;bt++)
{
buttonValues[bt]=newJRadioButtonMenuItem(content[bt]);
buttonGroup.add(buttonValues[bt]);
choiceMenu.add(buttonValues[bt]);
}
buttonValues[0].setSelected(true);
choiceMenu.addSeparator();
/*loopItem=newJCheckBoxMenuItem("是否循环");
choiceMenu.add(loopItem);
loopItem.addItemListener(newItemListener()
{
publicvoiditemStateChanged(ItemEvente)
{
loop=!
loop;
}
}
);*/
infor=newJMenuItem("软件简介");
aboutMenu.add(infor);
infor.addActionListener(this);
about=newJMenuItem("关于作者");
about.addActionListener(this);
aboutMenu.add(about);
//菜单栏设置完毕
panel=newJPanel();
panel.setLayout(newBorderLayout());
c.add(panel,BorderLayout.CENTER);
panelSouth=newJPanel();
panelSouth.setLayout(newBorderLayout());
c.add(panelSouth,BorderLayout.SOUTH);
icon=newImageIcon("icon\\Player.jpg");
label=newJLabel(icon);
panel.add(label);
popupMenu=newJPopupMenu();
del =newJMenuItem("删除");//鼠标右键弹出菜单对象实例化
popupMenu.add(del);
del.addActionListener(this);
delAll =newJMenuItem("全部删除");
popupMenu.add(delAll);
delAll.addActionListener(this);
reName =newJMenuItem("重命名");
popupMenu.add(reName);
reName.addActionListener(this);
scroll=newJScrollPane(list);//用于存放播放列表
listB=newJLabel(newImageIcon("icon\\qingdan.gif"),SwingConstants.CENTER);
panelSouth.add(listB,BorderLayout.NORTH);
panelSouth.add(scroll,BorderLayout.CENTER);
dialog1=newDialogDemo(MediaPlayer.this,"软件说明");
this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);//设定窗口关闭方式
//this.setTitle("d");编译通过,说明可以再次设定标题
this.setLocation(400,250);//设定窗口出现的位置
//this.setSize(350,320);//窗口大小
setSize(350,330);
this.setResizable(false);//设置播放器不能随便调大小
this.setVisible(true);//此句不可少,否则窗口会不显示
}
publicvoidactionPerformed(ActionEvente)
{
if(e.getSource()==openItem)//getSource()判断发生时间的组键
{
//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,"此简易播放器由计科0302\n"
+"harly\n"+" 完成 ",
"参与者",
JOptionPane.INFORMATION_MESSAGE);
}
if(e.getSource()==del)
{
//index
//delPaintList(index);
fileName.removeElementAt(indexForDel);
dirName.removeElementAt(indexForDel);
numList.removeAllElements();//从三个容器里面移除此项
EnumerationenumFile=fileName.elements();
while(enumFile.hasMoreElements())
{
numList.addElement((numList.size()+1)+"."+enumFile.nextElement());
//numList添加元素,显示播放里表中
}
//list.setListData(fileName);
list.setListData(numList);
if(index 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)//重命名 { Stringname;//=JOptionPane.showInputDialog(this,"请输入新的名字"); try { name=reNames(); fileName.setElementAt(name,indexForDel); numList.setElementAt((indexForDel+1)+"."+name,indexForDel); } catch(ReNamee2)//自定义的异常 { } } if(e.getSource()==infor) { dialog1.setVisible(true); } } publicstaticvoidmain(String[]args) { finalMediaPlayermp=newMediaPlayer(); mp.setIconImage(newImageIcon("icon\\mPlayer.jpg").getImage());//改变默认图标 mp.addWindowListener(newWindowAdapter()//注册窗口事件 { publicvoidwindowClosing(WindowEvente) { //System.exit(0); mp.exity_n(); } } ); System.out.println("注意: 更新文件列表后,请先正常关闭播放器" +"\n然后再关闭此DOS窗口,否则导致播放列表不能保存! ! "); } privatevoidopenFile()//为了界面原因,此代码重写,估计兼容性不好了 { /*JFileChooserfileChooser=newJFileChooser();//文件选择器 fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);//可以选择文件不能目录 intresult=fileChooser.showOpenDialog(this);//创建文件打开对话框,并设定此程序为父窗口监控*/ /*通过result的值来判断文件是否打开成功 *JFileChooser类有很多静态成员变量 **/ /*if(result==JFileChooser.CANCEL_OPTION) { file=null;//file已经在类中定义,如果选择取消,file指向为空 } else { file=fileChooser.getSelectedFile();//获得文件对象 title=file.getAbsolutePath();//取得文件的绝对路径并且赋给title设定标题 }*/ //if(fd==null) //{ //Stringfilename="java音频播放器"; fd=newFileDialog(MediaPlayer.this); //Filtersfl=newFilters(); //fd.setFilenameFilter(fl); fd.setVisible(true); if(fd.getFile()! =null) { title=fd.getDirectory()+fd.getFile();//原因请见同目录下的FileDialogDemo.java文件 files=fd.getFile(); //dir=fd.getDirectory(); file=newFile(title); createPlayer(); } //title=filename; //fd=null;//缺少此句如果第一次打开文件的时候取消操作的时候第二次也不能打开文件了 //} } privatevoidopenDir() { JFileChooserfileChooser=newJFileChooser(); fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); intresult=fileChooser.showOpenDialog(MediaPlayer.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(inti=0;i { fileName.addElement(sFiles[i]); numList.addElement((numList.size()+1)+"."+sFiles[i]); dirName.addElement(file.getAbsolutePath()+"\\"+sFiles[i]); } list.setListData(numList); /*fd=newFileDialog(MediaPlayer.this); fd.setVisible(true); if(fd.getDirectory()! =null) { FilefileDir=newFile(fd.getDirectory()); String[]ss=fileDir.list(); for(inti=0;i { System.out.println(ss[i]); } }*/ } privatevoidcreatePlayer() { closePreviosPlayer();//关闭先前的媒体播放器 StringextendName="此播放器好象不支持"+title.substring(title.lastIndexOf(".")+1)+"格式"; try { player=Manager.createPlayer(file.toURL());//javax.media.Manager直接继承于java.lang.object,且它为final,不能被继承 player.addControllerListener(newControllerHand()); player.start(); addList(files); index=fileName.size()-1; list.setSelectedValue(numList.elementAt(index),true); //list.setSelectedIndex(index); //list.setSelectionForeground(Color.blue); setTitle(title); //addList("files");//到播放清单 //title="file.getAbsoluteFile()"; } catch(Exceptione) { JOptionPane.showMessageDialog(this,extendName,"出错了! ! ",JOptionPane.ERROR_MESSAGE); setTitle(extendName); } } privatevoidclosePreviosPlayer() { if(player==null) return; //player.close();//首先停止播放 /* *不能用上面的代码停止,要用下面的两行取代,否则Componentvisual=player.getVisualComponent();发生异常 **/ player.stop(); player.deallocate();//停止播放并且重新装载DateSource Componentvisual=player.getVisualComponent(); Componentcontrol=player.getControlPanelComponent(); if(visual! =null) { panel.remove(visual); } if(control! =null) { panel.remove(control); } } privateclassControllerHandimplementsControllerListener { publicvoidcontrollerUpdate(ControllerEvente) { if(einstanceofRealizeCompleteEvent) { Compone
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- myeclipse 编写 视频 播放 源代码