java中我想实现用按钮来控制音乐播放与停止 下面是我的代码 但实现不了播放和停止 谁能帮我解决一下吗?
importjava.applet.Applet;importjava.applet.AudioClip;importjava.awt.Container;importj...
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.URI;
import java.net.URL;
import javax.sound.sampled.Clip;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class f extends JFrame implements ActionListener {
private static final String AudioClip = null;
private JButton Oj;
private JButton Oj1;
File musicFile;
URI uri;
URL url;
private void f(int a) throws InterruptedException{
musicFile = new File("E:\\JAVA\\new1\\celine dion - falling into you.wav");
uri = musicFile.toURI();
try {
url = uri.toURL();
} catch (Exception e) {
}
AudioClip clip = Applet.newAudioClip(url);
clip.play();
long timeout = 1;
clip.wait(timeout);
}
f() {
this.setSize(800, 600);
this.setResizable(false);
JPanel p = new JPanel();
this.setContentPane(p);
this.setVisible(true);
Oj = new JButton("开始");
Oj1 = new JButton("结束");
this.setVisible(true);
Oj.addActionListener(this);
Oj1.addActionListener(this);
this.add(Oj);
this.add(Oj1);
}
public static void main(String[] args) throws InterruptedException {
new f();
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == Oj) {
System.out.println("进入游戏界面");
play();
} else if (e.getSource() == Oj1) {
System.out.println("退出游戏");
((java.applet.AudioClip) musicFile).stop();
//System.exit(0);
}
}
public void play() {
if (musicFile != null)
( (java.applet.AudioClip) musicFile).play();
}
public void stop() {
if (musicFile != null)
( (java.applet.AudioClip) musicFile).stop();
}
} 展开
import java.applet.AudioClip;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.URI;
import java.net.URL;
import javax.sound.sampled.Clip;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class f extends JFrame implements ActionListener {
private static final String AudioClip = null;
private JButton Oj;
private JButton Oj1;
File musicFile;
URI uri;
URL url;
private void f(int a) throws InterruptedException{
musicFile = new File("E:\\JAVA\\new1\\celine dion - falling into you.wav");
uri = musicFile.toURI();
try {
url = uri.toURL();
} catch (Exception e) {
}
AudioClip clip = Applet.newAudioClip(url);
clip.play();
long timeout = 1;
clip.wait(timeout);
}
f() {
this.setSize(800, 600);
this.setResizable(false);
JPanel p = new JPanel();
this.setContentPane(p);
this.setVisible(true);
Oj = new JButton("开始");
Oj1 = new JButton("结束");
this.setVisible(true);
Oj.addActionListener(this);
Oj1.addActionListener(this);
this.add(Oj);
this.add(Oj1);
}
public static void main(String[] args) throws InterruptedException {
new f();
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == Oj) {
System.out.println("进入游戏界面");
play();
} else if (e.getSource() == Oj1) {
System.out.println("退出游戏");
((java.applet.AudioClip) musicFile).stop();
//System.exit(0);
}
}
public void play() {
if (musicFile != null)
( (java.applet.AudioClip) musicFile).play();
}
public void stop() {
if (musicFile != null)
( (java.applet.AudioClip) musicFile).stop();
}
} 展开
1个回答
展开全部
我帮你把程序改完了,可以实现用按钮来控制音乐播放与停止了,你看看吧。
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.URI;
import java.net.URL;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class f extends JFrame implements ActionListener {
private static final String AudioClip = null;
private JButton Oj;
private JButton Oj1;
AudioClip clip =null;
File musicFile;
URI uri;
URL url;
private void f() throws InterruptedException{
musicFile = new File("E:\\JAVA\\new1\\celine dion - falling into you.wav");
uri = musicFile.toURI();
try {
url = uri.toURL();
} catch (Exception e) {
}
clip=Applet.newAudioClip(url);
//clip.play();
}
f() {
this.setSize(800, 600);
this.setResizable(false);
JPanel p = new JPanel();
this.setContentPane(p);
this.setVisible(true);
Oj = new JButton("开始");
Oj1 = new JButton("结束");
this.setVisible(true);
Oj.addActionListener(this);
Oj1.addActionListener(this);
this.add(Oj);
this.add(Oj1);
}
public static void main(String[] args) throws InterruptedException {
f ff=new f();
ff.f();
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == Oj) {
System.out.println("进入游戏界面");
play();
} else if (e.getSource() == Oj1) {
System.out.println("退出游戏");
stop();
//System.exit(0);
}
}
public void play() {
if (clip != null)
( (java.applet.AudioClip) clip).play();
}
public void stop() {
if (clip != null)
( (java.applet.AudioClip) clip).stop();
}
}
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.URI;
import java.net.URL;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class f extends JFrame implements ActionListener {
private static final String AudioClip = null;
private JButton Oj;
private JButton Oj1;
AudioClip clip =null;
File musicFile;
URI uri;
URL url;
private void f() throws InterruptedException{
musicFile = new File("E:\\JAVA\\new1\\celine dion - falling into you.wav");
uri = musicFile.toURI();
try {
url = uri.toURL();
} catch (Exception e) {
}
clip=Applet.newAudioClip(url);
//clip.play();
}
f() {
this.setSize(800, 600);
this.setResizable(false);
JPanel p = new JPanel();
this.setContentPane(p);
this.setVisible(true);
Oj = new JButton("开始");
Oj1 = new JButton("结束");
this.setVisible(true);
Oj.addActionListener(this);
Oj1.addActionListener(this);
this.add(Oj);
this.add(Oj1);
}
public static void main(String[] args) throws InterruptedException {
f ff=new f();
ff.f();
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == Oj) {
System.out.println("进入游戏界面");
play();
} else if (e.getSource() == Oj1) {
System.out.println("退出游戏");
stop();
//System.exit(0);
}
}
public void play() {
if (clip != null)
( (java.applet.AudioClip) clip).play();
}
public void stop() {
if (clip != null)
( (java.applet.AudioClip) clip).stop();
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询