import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.URL;
import java.nio.file.Path;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
public class SoundMusic_5 implements ActionListener
{
AudioClip ac=null;
File file=null;
JFileChooser jfc=new JFileChooser();
public SoundMusic_5()
{
JFrame jf=new JFrame("音乐播放器");
JPanel jp=new JPanel();
JMenuBar jmb=new JMenuBar();
JMenu jm=new JMenu("添加曲目");
JMenuItem jmi1=new JMenuItem("打开");
JMenuItem jmi2=new JMenuItem("退出");
JButton b1= new JButton("开始");
JButton b2= new JButton("停止");
JButton b3= new JButton("循环");
jp.setLayout(new FlowLayout());
jp.add(b1);
b1.addActionListener(this);
jp.add(b2);
b2.addActionListener(this);
jp.add(b3);
b3.addActionListener(this);
jmb.add(jm);
jm.add(jmi1);
jmi1.addActionListener(this);
jm.add(jmi2);
jmi2.addActionListener(this);
jf.setJMenuBar(jmb);
jf.add(jp,BorderLayout.SOUTH);
jf.setPreferredSize(new Dimension(250, 300));
jf.pack();
jf.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("打开"))
{
jfc.setCurrentDirectory(new File("E:/"));
jfc.showOpenDialog(null);
try
{
file = jfc.getSelectedFile();
URL url=new URL(file.getPath());
ac=Applet.newAudioClip(url);
ac.play();
}
catch(Exception e1)
{
}
if(e.getActionCommand().equals("开始"))
{
if(ac==null)
return;
ac.play();
}
}
if(e.getActionCommand().equals("停止"))
{
ac.stop();
}
}
public static void main(String []agrs) throws NullPointerException
{
new SoundMusic_5();
}
}
