Java写的音乐播放器播放不了

Justin~ 2017-05-16 10:37:58
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();



}



}
...全文
231 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
逗泥丸的平方 2017-05-17
  • 打赏
  • 举报
回复
actionPerformed 里面有空指针 先debug看一下吧.. 你这样贴的代码也看不清是哪一行 (不过你自己应该能找到)
  • 打赏
  • 举报
回复
没用过Java写播放器。。。 参考下别人写的吧

51,409

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧