📄 example24_2.java
字号:
import java.applet.*;import java.awt.*;import java.awt.event.*;
public class Example24_2 extends Applet implements ActionListener,Runnable,ItemListener
{ AudioClip clip;//声明一个音频对象。
Choice choice;
TextField text;
Thread thread;
String item=null;
Button button_play,button_loop,button_stop;
public void init()
{ choice=new Choice();
thread=new Thread(this);
int N=Integer.parseInt(getParameter("总数"));
for(int i=1;i<=N;i++)
{ choice.add(getParameter(String.valueOf(i)));
}
button_play=new Button("开始播放");
button_loop=new Button("循环播放");
button_stop=new Button("停止播放");
text=new TextField(12);
button_play.addActionListener(this);
button_stop.addActionListener(this);
button_loop.addActionListener(this);
choice.addItemListener(this);
add(choice);
add(button_play);add(button_loop);add(button_stop); add(text);
button_play.setEnabled(false);
button_loop.setEnabled(false);
}
public void itemStateChanged(ItemEvent e)
{ item=choice.getSelectedItem();
int index=item.indexOf(":");
item=item.substring(index+1).trim();
if(!(thread.isAlive()))
{ thread=new Thread(this);
}
try {
thread.start();
}
catch(Exception exp)
{ text.setText("正在下载音频文件");
}
}
public void stop()
{ clip.stop();
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==button_play)
{ clip.play();
}
else if(e.getSource()==button_loop)
{ clip.loop();
}
else if(e.getSource()==button_stop)
{ clip.stop();
button_play.setEnabled(false);
button_loop.setEnabled(false);
}
}
public void run()
{
clip=getAudioClip(getCodeBase(),item);
text.setText("请稍等...");
if(clip!=null)
{ button_play.setEnabled(true);
button_loop.setEnabled(true);
text.setText("您可以播放了");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -