【Java精品源码栏目提醒】:网学会员,鉴于大家对Java精品源码十分关注,论文会员在此为大家搜集整理了“【精品】myeclipse编写的视频播放器源代码 - 大学课件”一文,供大家参考学习!
第一步:我用的是 MyEclipse 5.5。
所以,先建立一个 class 文件,名字:MediaPlayer.
java完整代码如下:import
java.awt.import
java.awt.event.import javax.swing.import javax.media.import
java.io.import
java.util.//为了导入 Vectorpublic class MediaPlayer extends JFrame implements ActionListenerRunnableprivate JMenuBar bar//菜单条private JMenu fileMenuchoiceMenuaboutMenuprivate JMenuItem openItemopenDirItemcloseItemaboutinforprivate JCheckBoxMenuItem onTopprivate boolean topfalseloop//设定窗口是否在最前面private Player player//Play 是个实现 Controller 的接口private File filelistFile//利用 File 类结合 JFileChooser 进行文件打开操作后则与 list.ini 有关private Container c//private UIManager.LookAndFeelInfo lookprivate String titlelistIniAddress//标题private FileDialog fdprivate JPanel panelpanelSouthprivate Icon icon //开始进入的时候要显示的图标,它为抽象类,不能自己创建private JLabel//listBnew JLabelnew ImageIconquotqingdan.gifquotSwingConstants.CENTERscrollnew JScrollPanelist//用于存放播放列表//dialog2new JDialogTestMediaPlayer.thisquot播放清单quotscroll//dialog2.setVisibletruereadToList.start//启动先程,加载播放列表try Thread.sleep10catchInterruptedException e e.printStackTrace/lookUIManager.getInstalledLookAndFeelstry UIManager.setLookAndFeellook2.getClassName SwingUtilities.updateComponentTreeUIthiscatchException e e.printStackTrace///与下面的代码实现相同的功能但执行速度要慢,原因:明显转了个大弯/try UIManager.setLookAndFeelUIManager.getSystemLookAndFeelClassNamecatchException e e.printStackTrace ///此段代码使执行速度大大降低barnew JMenuBarsetJMenuBarbar//此两行创建菜单栏并放到此窗口程序//bar.setBackgroundnew Color4891183fileMenunew JMenuquot文件quotbar.addfileMenuchoiceMenunew JMenuquot控制quotbar.addchoiceMenuaboutMenunew JMenuquot帮助quotbar.addaboutMenuopenItem new JMenuItemquot打开文件quotopenDirItem new JMenuItemquot打开目录quotcloseItem new JMenuItemquot退出程序quotopenItem.addActionListenerthisopenDirItem.addActionListenerthiscloseItem.addActionListenerthisfileMenu.addopenItemfileMenu.addopenDirItemfileMenu.addcloseItemonTopnew JCheckBoxMenuItemquot播放时位于最前面quottopchoiceMenu.addonToponTop.addItemListenernew ItemListener public void itemStateChangedItemEvent e ifonTop.isSelected toptrue else topfalse setAlwaysOnToptop choiceMenu.addSeparator//加分割符号buttonGroupnew ButtonGroupbuttonValuesnew JRadioButtonMenuItem3forint bt0btlt3bt buttonValuesbtnew JRadioButtonMenuItemcontentbt buttonGroup.addbuttonValuesbt choiceMenu.addbuttonValuesbtbuttonValues0.setSelectedtruechoiceMenu.addSeparator/loopItemnew JCheckBoxMenuItemquot是否循环quotchoiceMenu.addloopItemloopItem.addItemListenernew ItemListener public void itemStateChangedItemEvent e looploop /infornew JMenuItemquot软件简介quotaboutMenu.addinforinfor.addActionListenerthis aboutnew JMenuItemquot关于作者quotabout.addActionListenerthisaboutMenu.addabout//菜单栏设置完毕panelnew JPanelpanel.setLayoutnew BorderLayoutc.addpanelBorderLayout.CENTERpanelSouthnew JPanelpanelSouth.setLayoutnew BorderLayoutc.addpanelSouthBorderLayout.SOUTHiconnew ImageIconquoticonPlayer.jpgquotlabelnew JLabeliconpanel.addlabelpopupMenunew JPopupMenudel new JMenuItemquot删除quot//鼠标右键弹出菜单对象实例化popupMenu.adddeldel.addActionListenerthisdelAll new JMenuItemquot全部删除quotpopupMenu.adddelAlldelAll.addActionListenerthisreName new JMenuItemquot重命名quotpopupMenu.addreNamereName.addActionListenerthisscrollnew JScrollPanelist//用于存放播放列表listBnew JLabelnew ImageIconquoticonqingdan.gifquotSwingConstants.CENTERpanelSouth.addlistBBorderLayout.NORTHpanelSouth.addscrollBorderLayout.CENTERdialog1new DialogDemoMediaPlayer.thisquot软件说明quotthis.setDefaultCloseOperationWindowConstants.DO_NOTHING_ON_CLOSE//设定窗口关闭方式//this.setTitlequotdquot编译通过,说明可以再次设定标题this.setLocation400250//设定窗口出现的位置//this.setSize350320//窗口大小setSize350330this.setResizablefalse//设置播放器不能随便调大小this.setVisibletrue//此句不可少,否则窗口会不显示public void actionPerformedActionEvent eife.getSourceopenItem//getSource判断发生时间的组键 //System.out.printlnquotdquot测试用 openFile //createPlayer //setTitletitleife.getSourceopenDirItem//打开目录 openDirife.getSourcecloseItem//推出播放器 exity_n //System.exit0ife.getSourceabout JOptionPane.showMessageDialogthisquot此简易播放器由计科 0302nquot quotharlyn quotquot 完成 quot quot参与者quot JOptionPane.INFORMATION_MESSAGEife.getSourcedel //index //delPaintListindex fileName.removeElementAtindexForDel dirName.removeElementAtindexForDel numList.removeAllElements//从三个容器里面移除此项 Enumeration enumFilefileName.elements whileenumFile.hasMoreElements numList.addElementnumList.size1quot.quotenumFile.nextElement //numList 添加元素,显示播放里表中 //list.setListDatafileName list.setListDatanumList ifindexltindexForDel list.setSelectedValuenumList.elementAtindextrue else ifindexindexForDel else ifindex0 list.setSelectedValuenumList.elementAtindex-1true //list.setSelectedIndexindexife.getSourcedelAll//全部删除 fileName.removeAllElements dirName.removeAllElements numList.removeAllElements list.setListDatanumListife.getSourcereName//重命名 String name//JOptionPane.showInputDialogthisquot请输入新的名字quot try namereNames fileName.setElementAtnameindexForDel numList.setElementAtindexForDel1quot.quotnameindexForDel catchReName e2//自定义的异常 ife.getSourceinfor dialog1.setVisibletruepublic static void mainString argsfinal MediaPlayer mpnew MediaPlayermp.setIconImagenew ImageIconquoticonmPlayer.jpgquot.getImage//改变默认图标mp.addWindowListenernew WindowAdapter//注册窗口事件 public void windowClosingWindowEvent e //System.exit0 mp.exity_n System.out.printlnquot注意:更新文件列表后,请先正常关闭播放器quotquotn 然后再关闭此 DOS 窗口,否则导致播放列表不能保存!!quotprivate void openFile//为了界面原因,此代码重写,估计兼容性不好了/JFileChooser fileChoosernew JFileChooser//文件选择器fileChooser.setFileSelectionModeJFileChooser.FILES_ONLY//可以选择文件不能目录int resultfileChooser.showOpenDialogthis//创建文件打开对话框,并设定此程序为父窗口监控//通过 result 的值来判断文件是否打开成功 JFileChooser 类有很多静态成员变量 / /ifresultJFileChooser.CANCEL_OPTION filenull//file 已经在类中定义,如果选择取消,file 指向为空 else filefileChooser.getSelectedFile//获得文件对象 titlefile.getAbsolutePath//取得文件的绝对路径并且赋给 title 设定标题 / //iffdnull // //String filenamequotjava 音频播放器quot fd new FileDialogMediaPlayer.this //Filters flnew Filters //fd.setFilenameFilterfl fd.setVisibletrue if fd.getFile null title fd.getDirectory fd.getFile//原因请见同目录下的FileDialogDemo.
java 文件 filesfd.getFile //dir fd.getDirectory filenew Filetitle createPlayer //titlefilename //fdnull//缺少此句如果第一次打开文件的时候取消操作的时候第二次也不能打开文件了//private void openDirJFileChooser fileChoosernew JFileChooserfileChooser.setFileSelectionModeJFileChooser.DIRECTORIES_ONLYint resultfileChooser.showOpenDialogMediaPlayer.thisifresultJFileChooser.CANCEL_OPTIONreturnfilefileChooser.getSelectedFileiffilenullfile.getName.equalsquotquotJOptionPane.showMessageDialogthisquot错误的路径quotquot出错了quotJOptionPane.ERROR_MESSAGEString sFilesfile.listforint i0iltsFiles.lengthi fileName.addElementsFilesi numList.addElementnumList.size1quot.quotsFilesi dirName.addElementfile.getAbsolutePathquotquotsFilesilist.setListDatanumList/fdnew FileDialogMediaPlayer.thisfd.setVisibletrueiffd.getDirectorynull File fileDirnew Filefd.getDirectory String ssfileDir.list forint i0iltss.lengthi System.out.printlnssi /private void createPlayerclosePreviosPlayer//关闭先前的媒体播放器String extendNamequot此播放器好象不支持quottitle.substringtitle.lastIndexOfquot.quot1quot格式quottry playerManager.createPlayerfile.toURL//javax.media.Manager 直接继承于
java.lang.object且它为 final不能被继承 player.addControllerListenernew ControllerHand player.start addListfiles indexfileName.size-1 list.setSelectedValuenumList.elementAtindextrue //list.setSelectedIndexindex //list.setSelectionForegroundColor.blue setTitletitle //addListquotfilesquot//到播放清单 //titlequotfile.getAbsoluteFilequotcatchException e JOptionPane.showMessageDialogthisextendNamequot出错了quotJOptionPane.ERROR_MESSAGE setTitleextendNameprivate void closePreviosPlayerifplayernullreturn//player.close//首先停止播放/ 不能用上面的代码停止,要用下面的两行取代,否则 Component visualplayer.getVisualComponent发生异常 /player.stop player.deallocate //停止播放并且重新装载 DateSourceComponent visual player.getVisualComponentComponent controlplayer.getControlPanelComponentifvisualnull panel.removevisualifcontrolnull panel.removecontrolprivate class ControllerHand implements ControllerListenerpublic void controllerUpdateControllerEvent e ife instanceof RealizeCompleteEvent Component visualplayer.getVisualComponent ifvisualnull //System.out.printlnquot音频播放器不支持视频图象功能quot //setTitlequot音频播放器不支持视频图象功能quot panel.removeAll panel.addvisualBorderLayout.CENTER else panel.addlabelBorderLayout.CENTER ///此 else 语句可以防止因为原来播放视频图象后以后没有界面 Component controlplayer.getControlPanelComponent ifcontrolnull panel.addcontrolBorderLayout.SOUTH //c.validate panel.doLayout return if e instanceof EndOfMediaEvent /if loop player.setMediaTime new Time 0 player.start return/ ifbuttonValues0.isSelected iffileName.size0 return indexintMath.randomfileName.size ifbuttonValues1.isSelected iffileName.size0 return//必须有此 if 语句,否则当用户把播放列表清空的时候发生异常,偶然的机会发现的 //现在感觉测试软件真是太重要了,看来以后程序做好后要反复测试,考虑各种情况 indexindex1fileName.size ifbuttonValues2.isSelected player.setMediaTime new Time 0 player.start createPlayer2 private void exity_n/int exiexiJOptionPane.showConfirmDialogthisquot真的要离开么?quotquot退出程序quotJOptionPane.YES_NO_OPTIONJOptionPane.QUESTION_MESSAGE//ifexinullifexiJOptionPane.YES_OPTION saveList System.exit0return/saveListSystem.exit0private void addListString vf//fileReadListnew fileReadListfdd//try// //int i0 //fileName.addElementfileName.size1quot.quotvf fileName.addElementvf numList.addElementnumList.size1quot.quotvf //fileName.addElementiquot.quotvf dirName.addElementtitle list.setListDatanumList///catchException e e.printStackTrace //System.out.printlne.getMessage/private void createPlayer2trytitledirName.elementAtindex.toString//titledirName.elementAtindex.toStringcatchArrayIndexOutOfBoundsException ereturnfilenew FiletitleclosePreviosPlayer//关闭先前的媒体播放器String extendNamequot此播放器好象不支持quottitle.substringtitle.lastIndexOfquot.quot1quot格式quottry playerManager.createPlayerfile.toURL//javax.media.Manager 直接继承于
java.lang.object且它为 final不能被继承 player.addControllerListenernew ControllerHand player.start //list.setSelectedIndexindex list.setSelectedValuenumList.elementAtindextrue //list.setSelectionForegroundColor.blue //list.setSelectedIndexindex //addListfiles setTitletitle //addListquotfilesquot//到播放清单catchException e //JOptionPane.showMessageDialogthisextendNamequot出错了quotJOptionPane.ERROR_MESSAGE //setTitleextendName String exnull tryexfileName.elementAtindex.toString catchException e1return fileName.removeElementAtindex numList.removeAllElements Enumeration enumFilefileName.elements whileenumFile.hasMoreElements numList.addElementnumList.size1quot.quotenumFile.nextElement dirName.removeElementAtindex //list.setListDatafileName list.setListDatanumList System.out.printlnquot已经从播放列表中删除 quotquotquotquotexquotquotquotquot 文件quot quot因为此播放器不支持quotex.substringex.lastIndexOfquot.quot1quot格式quot quot不过没有从硬盘真正删除quot ifnumList.size0 indexnumList.size createPlayer2 private void saveListEnumeration enumFilefileName.elementsEnumeration enumDir dirName.elementstry outputnew ObjectOutputStreamnew BufferedOutputStreamnewFileOutputStreamlistFile whileenumFile.hasMoreElements listWriteFilenewListValuesenumFile.nextElement.toStringenumDir.nextElement.toString output.writeObjectlistWriteFile output.flush output.closecatchException e e.printStackTrace/finally output.flush output.close//郁闷,这两行不能写在这里,实在是一大遗憾啊,不知道有什么别的方法/public void runtry Thread.sleep1catchInterruptedExcept.
上一篇:
【精品】PhoneME简介
下一篇:
成本管理背景指导