mp3播放器

来源:互联网 发布:2015网络作家排行榜 编辑:程序博客网 时间:2024/05/01 23:38

package player;

import javax.media.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class MediaPlayer extends Frame implements ActionListener,

ControllerListener, ItemListener

{

Player player;

Component vc, cc;

boolean first = true, loop = false;

String currentDirectory;

MediaPlayer (String title)

{

super (title);

addWindowListener

(new WindowAdapter ()

{

public void windowClosing (WindowEvent e)

{

// 用户点击窗口系统菜单的关闭按钮

// 调用dispose以执行windowClosed

dispose ();

}

public void windowClosed (WindowEvent e)

{

if (player != null) player.close ();

System.exit (0);

}

});

Menu m = new Menu ("文件");

MenuItem mi = new MenuItem ("打开");

mi.addActionListener (this);

m.add (mi);

m.addSeparator ();

CheckboxMenuItem cbmi = new CheckboxMenuItem ("循环", false);

cbmi.addItemListener (this);

m.add (cbmi);

m.addSeparator ();

mi = new MenuItem ("退出");

mi.addActionListener (this);

m.add (mi);

Menu help=new Menu("帮助");
MenuItem about=new MenuItem("关于");
help.add(about);
about.addActionListener(this);

MenuBar mb = new MenuBar ();

mb.add (m);
mb.add(help);

setMenuBar (mb);
setBounds(400,200,200,200);
setVisible (true);

}

public void actionPerformed (ActionEvent e)

{


if (e.getActionCommand ().equals ("退出"))

{

// 调用dispose以便执行windowClosed

dispose ();

return;

}
if(e.getActionCommand().equals("关于"))
{
 about_win mc=new about_win(this); 
}
if(e.getActionCommand().equals("打开"))
{
 FileDialog fd = new FileDialog (this, "打开媒体文件",

FileDialog.LOAD);

fd.setDirectory (currentDirectory);

fd.show ();

// 如果用户放弃选择文件,则返回

if (fd.getFile () == null) return;

currentDirectory = fd.getDirectory ();

if (player != null)

player.close ();

try

{

player = Manager.createPlayer (new MediaLocator

("file:" + fd.getDirectory () + fd.getFile ()));

}

catch (java.io.IOException e2)

{

System.out.println (e2);

return;

}

catch (NoPlayerException e2)

{

System.out.println ("不能找到播放器.");

return;

}

if (player == null)

{

System.out.println ("无法创建播放器.");

return;

}

first = false;

setTitle (fd.getFile ());

player.addControllerListener (this);

player.prefetch ();
}
}

public void controllerUpdate (ControllerEvent e)

{

// 调用player.close()时ControllerClosedEvent事件出现。

// 如果存在视觉部件,则该部件应该拆除(为一致起见,

// 我们对控制面板部件也执行同样的操作)

if (e instanceof ControllerClosedEvent)

{

if (vc != null)

{

remove (vc);

vc = null;

}

if (cc != null)

{

remove (cc);

cc = null;

}

return;

}

if (e instanceof EndOfMediaEvent)

{

if (loop)

{

player.setMediaTime (new Time (0));

player.start ();

}

return;

}

if (e instanceof PrefetchCompleteEvent)

{

player.start ();

return;

}

if (e instanceof RealizeCompleteEvent)

{

vc = player.getVisualComponent ();

if (vc != null)

add (vc);

cc = player.getControlPanelComponent ();

if (cc != null)

add (cc, BorderLayout.SOUTH);

pack ();

}

}

public void itemStateChanged (ItemEvent e)

{

loop = !loop;

}

public void paint (Graphics g)

{

if (first)

{

int w = getSize ().width;

int h = getSize ().height;

g.setColor (Color.blue);

g.fillRect (0, 0, w, h);

Font f = new Font ("DialogInput", Font.BOLD, 16);

g.setFont (f);

FontMetrics fm = g.getFontMetrics ();

int swidth = fm.stringWidth ("*** MP3播放器 ***");

g.setColor (Color.white);

g.drawString ("*** MP3播放器 ***",

(w - swidth) / 2,

(h + getInsets ().top) / 2);

}

// 调用超类Frame的paint()方法,该paint()方法将调用Frame包含的各个容器

// 和部件(包括控制面板部件)的paint()方法。

super.paint (g);

}

// 不执行背景清除操作,以免控制面板部件闪烁

public void update (Graphics g)

{

paint (g);

}

public static void main (String [] args)

{

new MediaPlayer ("MP3播放器");

}

}

class about_win extends Frame
{
 MediaPlayer father;
 public about_win(MediaPlayer t)
 {
 
  this.father=t;
  t.setEnabled(false);
  //Container c = this.getContentPane();
  this.setLayout(null);
  Label jl =new Label("CopyRight@Laou2008");
  jl.setBounds(10,20,120,60);
  this.add(jl);
  this.setResizable(false);
  this.setSize(140,80);
  this.setLocation(450,200);
  this.addWindowListener
    (
     new WindowAdapter()
     {
      public void windowClosing(WindowEvent e)
      {
       about_win.this.father.setEnabled(true);
       about_win.this.dispose();
      }
     }
   );
  this.setVisible(true);
 }
}

原创粉丝点击