import java.applet.*;
import java.awt.event.*;
import java.awt.*;
@SuppressWarnings("serial")
public class music extends Applet
implements ActionListener{
AudioClip audio;
Button btExit,btOpen,btPlay,btLoop,btStop;
public void init() {
//加载声音文件
audio = getAudioClip(getDocumentBase(),"music.au");//这里放上你加在新建文件夹里面的歌曲名称,au格式
//构造按钮
setLayout(new FlowLayout());
btPlay=new Button("Play");
btPlay.addActionListener(this);
btLoop=new Button("Loop");
btLoop.addActionListener(this);
btStop=new Button("Stop");
btStop.addActionListener(this); //给Play按钮添加一个监听事件
//将按钮添加到Applet中
add(btPlay);
add(btLoop);
add(btStop);
}
public void actionPerformed(ActionEvent e) {
//如果点击的是Play按钮
if (e.getSource()==btPlay) {
play();
}
//如果点击的是loop按钮
if (e.getSource()==btLoop) {
loop();
}
//如果点击的是stop按钮
if (e.getSource()==btStop) {
stop();
}
}
public void play(){
if (audio!=null) stop();
audio.play();
}
public void loop(){
if (audio!=null)
audio.loop();
}
public void stop(){
if (audio!=null)
audio.stop();
}
}