java 代码
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import javax.media.*;
- public class VideoDemo extends JFrame implements ActionListener,ControllerListener
- {
- JMenuBar jMenuBar1=new JMenuBar();
- JMenu jMenuFile=new JMenu( "File" );
- JMenu jMenuList=new JMenu( "List" );
- JMenu jMenuControl=new JMenu( "Control" );
- JMenu jMenuSkin=new JMenu( "Skin" );
- JMenu jMenuHelp=new JMenu( "Help" );
- JMenuItem jMenuFileOpen=new JMenuItem( "Open" );
- JMenuItem jMenuFileExit=new JMenuItem( "Exit" );
- JMenuItem jMenuControlPlay=new JMenuItem( "Play" );
- JMenuItem jMenuControlPause=new JMenuItem( "Pause" );
- JMenuItem jMenuControlStop=new JMenuItem( "Stop" );
- JMenuItem jMenuSkinmetal=new JMenuItem( "metalSkin" );
- JMenuItem jMenuSkinwindows=new JMenuItem( "windowsSkin" );
- JMenuItem jMenuSkinmotif=new JMenuItem( "motifSkin" );
- JMenuItem jMenuControlFullScreen=new JMenuItem( "FullScreen" );
- JLabel statusBar=new JLabel();
- JLabel welcome=new JLabel();
- Player player;
- Component visualComponent;//Video player component
- Component controlComponent;//video player control component
- JPanel jPanel1=new JPanel( new BorderLayout());
- String file="" ;
- String strDir;
- public VideoDemo()
- {
- this .setSize( 400 , 300 );
- this .setTitle( "Casper's player" );
- welcome=new JLabel( new ImageIcon( "girl.jpg" ));
- jPanel1.add(welcome);
- statusBar.setText(" " );
- jMenuFileExit.addActionListener(this );
- jMenuFileOpen.addActionListener(this );
- jMenuFile.add(jMenuFileOpen);
- jMenuFile.addSeparator();
- jMenuFile.add(jMenuFileExit);
- jMenuBar1.add(jMenuFile);
- jMenuBar1.add(jMenuSkin);
- jMenuSkin.add(jMenuSkinmetal);
- jMenuSkinmetal.addActionListener(this );
- jMenuSkin.add(jMenuSkinwindows);
- jMenuSkinwindows.addActionListener(this );
- jMenuSkin.add(jMenuSkinmotif);
- jMenuSkinmotif.addActionListener(this );
- jMenuBar1.add(jMenuControl);/
- jMenuControl.add(jMenuControlPlay);
- jMenuControlPlay.addActionListener(this );
- jMenuControl.add(jMenuControlPause);
- jMenuControlPause.addActionListener(this );
- jMenuControl.add(jMenuControlStop);
- jMenuControlStop.addActionListener(this );
- jMenuControl.add(jMenuControlFullScreen);///
- jMenuControlFullScreen.addActionListener(this );
- jMenuBar1.add(jMenuList);
- jMenuBar1.add(jMenuHelp);
- this .setJMenuBar(jMenuBar1);
- statusBar.setText("No file open" );
- Manager.setHint(Manager.LIGHTWEIGHT_RENDERER,new Boolean( true ));
- //
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this .getContentPane().add(statusBar, "South" );
- this .getContentPane().add(jPanel1, "Center" );
- this .setVisible( true );
- }
- public void actionPerformed(ActionEvent e)
- {
- //Skin menu(three skins)
- if (e.getSource()==jMenuSkinwindows)
- {
- String windows="com.sun.java.swing.plaf.windows.WindowsLookAndFeel" ;
- try {
- UIManager.setLookAndFeel(windows);//设置见面风格
- SwingUtilities.updateComponentTreeUI(this ); //使设置的界面风格生效
- this .pack();
- }catch (Exception e1){
- System.out.println("can't set such style" );
- }
- }
- else if (e.getSource()==jMenuSkinmetal)
- {
- String metal="javax.swing.plaf.metal.MetalLookAndFeel" ;
- try {
- UIManager.setLookAndFeel(metal);
- SwingUtilities.updateComponentTreeUI(this );
- this .pack();
- }catch (Exception e1){
- System.out.println("can't set such style" );
- }
- }
- else if (e.getSource()==jMenuSkinmotif)
- {
- String motif="com.sun.java.swing.plaf.motif.MotifLookAndFeel" ;
- try {
- UIManager.setLookAndFeel(motif);
- SwingUtilities.updateComponentTreeUI(this );
- this .pack();
- }catch (Exception e1){
- System.out.println("can't set such style" );
- }
- }
- //
- //File menu
- else if (e.getSource()==jMenuFileExit)
- {
- if (player!= null )
- player.close();
- this .dispose();
- System.exit(0 );
- }
- else if (e.getSource()==jMenuFileOpen)
- {
- try
- {
- jPanel1.remove(welcome);
- FileDialog dlgOpen=new FileDialog( this , "Open file" ,FileDialog.LOAD);
- dlgOpen.setDirectory(strDir);
- dlgOpen.setVisible(true );
- strDir=dlgOpen.getDirectory();
- if (strDir!= null )
- {
- if (player!= null )
- player.close();
- if (visualComponent!= null )
- jPanel1.remove(visualComponent);
- if (controlComponent!= null )
- jPanel1.remove(controlComponent);
- visualComponent=null ;
- controlComponent=null ;
- file=strDir+dlgOpen.getFile();
- // player=Manager.createPlayer(new URL("file:\\"+file));//create the object of the media will play
- player = Manager.createPlayer (new MediaLocator("file: ” +file));
- statusBar.setText(" playing: "+file.toString());//display the file imfomation
- player.addControllerListener(this);
- player.prefetch();//get the file data
- }
- }catch(Exception ex){ex.printStackTrace();}
- }
- //control funcitons Menu
- else if(e.getSource()==jMenuControlFullScreen)//Full screen
- {
- dispose();
- setUndecorated(true);
- getGraphicsConfiguration().getDevice().setFullScreenWindow(this);
- setVisible(true);
- }
- else if(e.getActionCommand().equals(" Play ")) //play
- {
- if(player != null)
- {
- player.start();
- }
- return;
- }
- else if(e.getActionCommand().equals(" Pause ")) //pause
- {
- if(player != null)
- {
- player.stop();
- }
- return;
- }
- else if(e.getActionCommand().equals(" Stop")) //stop
- {
- if (player != null )
- {
- player.stop();
- player.setMediaTime(new Time( 0 ));
- }
- return ;
- }
- }
- public void controllerUpdate(ControllerEvent e)
- {
- if (e instanceof PrefetchCompleteEvent)
- {
- player.start();
- return ;
- }
- if (e instanceof RealizeCompleteEvent)
- {
- visualComponent=player.getVisualComponent();
- if (visualComponent!= null )
- jPanel1.add(visualComponent,BorderLayout.CENTER);
- controlComponent=player.getControlPanelComponent();//time shaft control
- if (controlComponent!= null )
- jPanel1.add(controlComponent,BorderLayout.SOUTH);
- player.getGainControl(); //volume control conponent
- this .pack();
- this .setResizable( false );
- this .setVisible( true );
- return ;
- }
- }
- public static void main(String[] args)
- {new VideoDemo();}
- }
/*Player类中的很多方法只有在Player对象处于Realized的状态下才会被调用。
为了保证Player对象已经到达了该状态,
你需要使用Manager的createRealizePlayer()方法
来获得Player对象。但是对于start()方法来说,
你可以在Player对象到达Prefetched状态之前调用它,
它可以自动将Player的状态转换到Started状态*/
//Dimension screen=Toolkit.getDefaultToolkit().getScreenSize();//取出屏幕大小
//player.getGainControl().getControlComponent(); //是操作音量(增加)的可视组件volume control conponent