双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

java播放音频文件问题

vbvcvf2000 vbvcvf2000 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
java播放音频文件的一个简单应用,可是点“播放”菜单就是无反应,哪位能指点一下,谢谢!
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;
import java.net.*;

public class Mingqu implements ActionListener
{
JMenuItem menuItem1,menuItem2,menuItem3,menuItem4;
AudioClip sound=loadSound("a.wav");
static JFrame frame;
public static void main(String args[])
{
frame=new JFrame("歌曲播放");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Mingqu ming=new Mingqu();
frame.setJMenuBar(ming.init());
frame.setSize(250,150);
frame.setVisible(true);
}
public JMenuBar init()
{
JMenuBar menuBar=new JMenuBar();
JMenu menu=new JMenu("音乐");
menuItem1=new JMenuItem("播放");
menu.add(menuItem1);
menuItem2=new JMenuItem("循环");
menu.add(menuItem2);
menuItem3=new JMenuItem("停止");
menu.add(menuItem3);
menuItem4=new JMenuItem("退出");
menu.add(menuItem4);
menuBar.add(menu);
menuItem1.addActionListener(this);
menuItem2.addActionListener(this);
menuItem3.addActionListener(this);
menuItem4.addActionListener(this);
return menuBar;
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==menuItem1) sound.play();
if(e.getSource()==menuItem2) sound.loop();
if(e.getSource()==menuItem3) sound.stop();
if(e.getSource()==menuItem4) System.exit(0);
}
private AudioClip loadSound(String fileName)
{
URL url=null;
try{url=new URL("file:"+System.getProperty("user.dir")+"/"+fileName);}
catch(MalformedURLException e){}
return Applet.newAudioClip(url);
}
}
...全文
评论 4
收藏 已收藏
分享
返回顶部
vbvcvf2000
Eclipse 58,439 成员 49,457 内容