package guitest;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Button;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.LayoutManager;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.MalformedURLException;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
public class MusicPalyer extends JFrame{
File file;
TextField t=new TextField(20);
public MusicPalyer() {
this.add(b1());
this.add(t);
this.add(b2());
this.setSize(300, 100);
this.setVisible(true);
this.setLayout(new FlowLayout());
}
private Button b1(){
Button b=new Button("choose");
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser jf = new JFileChooser();
jf.showOpenDialog(null);
file = jf.getSelectedFile();
t.setText(file.getAbsolutePath());
}
});
return b;
}
private Button b2(){
Button b1=new Button("paly");
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(file!=null){
AudioClip au = null;
try {
if(au!=null){au.stop();}
au = Applet.newAudioClip(file.toURL());
au.play();
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
});
return b1;
}
public static void main(String[] args) {
new MusicPalyer();
}
}
如上,wav音频文件已选择好,为什么播放不了啊