⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mediaplayer.java

📁 一个JAVA做的浏览器
💻 JAVA
字号:
package player;

import javax.media.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

public class MediaPlayer extends JFrame implements ActionListener, ControllerListener, ItemListener
{

	Player player;
	Component vc, cc;
	boolean first = true, loop = false;
	String currentDirectory;

	MediaPlayer (String title)
	{
		super (title);

		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setAlwaysOnTop(true);
		this.setResizable(false);
		this.setBounds(400,200,200,200);


			JMenu m = new JMenu ("文件");
			JMenuItem mi = new JMenuItem ("打开");

			mi.addActionListener (this);
			m.add (mi);
			m.addSeparator ();

			JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem("循环", false);
			cbmi.addItemListener (this);

			m.add(cbmi);
			m.addSeparator ();
			mi = new JMenuItem ("退出");
			mi.addActionListener (this);
			m.add (mi);

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

			JMenuBar mb = new JMenuBar ();

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

			setJMenuBar (mb);

			this.setVisible(true);
	}

	public void actionPerformed (ActionEvent e)
	{
		//当点击"退出"的时候
		if (e.getActionCommand ().equals ("退出"))
		{
			System.exit(0);
		}
		//点击"关于"时,显示"关于"对话框
		if(e.getActionCommand().equals("关于"))
		{
			JOptionPane.showConfirmDialog(this,"Copyright by Piquancys\nversion:1.0.0","关于",JOptionPane.CLOSED_OPTION);
		}
		//点击"打开"按钮时,显示JFileChooser类型的对话框来打开一个文件
		if(e.getActionCommand().equals("打开"))
		{
			//创建并显示
			JFileChooser jfc = new JFileChooser();
			jfc.showOpenDialog(this);
			//如果没有选择文件,则返回
			if (jfc.getSelectedFile() == null)
				return;

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

			try
			{
				player = Manager.createPlayer (new MediaLocator("file:" + jfc.getSelectedFile()));
			}
			catch (IOException e2)
			{
				e2.printStackTrace();
			}
			catch (NoPlayerException e2)
			{
				e2.printStackTrace();
			}
			if (player == null)
			{
				return;
			}
			first = false;
			//将窗口的标题设置为播放文件的名字
			setTitle (jfc.getSelectedFile().toString());
			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.red);
			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.red);
			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播放器");
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -