📄 ex10_6.java
字号:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Ex10_6 extends JApplet {
AudioClip audio1, audio2, audio3, currentaudio;
JButton playaudio, loopaudio, stopaudio;
JComboBox choice;
public void init() {
Container container = getContentPane();
container.setLayout( new FlowLayout() );
String choices[] = { "Hi", "ring", "test" };
choice = new JComboBox( choices );
choice.addItemListener( new choiceListener());
container.add( choice );
ButtonHandler handler = new ButtonHandler();
playaudio = new JButton( "播放" );
loopaudio = new JButton( "循环" );
stopaudio = new JButton( "停止" );
playaudio.addActionListener( handler );
loopaudio.addActionListener( handler );
stopaudio.addActionListener( handler );
container.add( playaudio );
container.add( loopaudio );
container.add( stopaudio );
// 加载声音文件并设置当前的声音文件
audio1 = getAudioClip( getDocumentBase(), "hi.au" );
audio2 = getAudioClip( getDocumentBase(), "ring.wav" );
audio3 = getAudioClip( getDocumentBase(), "test.mid" );
currentaudio = audio1;
}
// 结束声音文件的播放
public void stop(){
currentaudio.stop();
}
// 下拉列表框的事件接受器
class choiceListener implements ItemListener {
// 停止当前声音文件的播放并设置当前声音文件为用户的选择
public void itemStateChanged( ItemEvent e ) {
currentaudio.stop();
switch (choice.getSelectedIndex()) {
case 0:
currentaudio =audio1;
break;
case 1:
currentaudio =audio2;
break;
case 2:
currentaudio =audio3;
break;
}
}
}
// 播放、循环和停止按钮事件接受器
private class ButtonHandler implements ActionListener {
public void actionPerformed( ActionEvent e ) {
if ( e.getSource() == playaudio )
currentaudio.play();
else if ( e.getSource() == loopaudio )
currentaudio.loop();
else if ( e.getSource() == stopaudio )
currentaudio.stop();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -