MusicApplet.java
上传用户:yunshuzx
上传日期:2022-04-09
资源大小:3k
文件大小:2k
- import java.io.*;
- import java.awt.*;
- import java.applet.*;
- import java.awt.event.*;
- public class MusicApplet extends Applet implements ActionListener{
- Button b_play, b_loop, b_stop;
- TextArea ta;
- List list;
- //audio
- AudioClip audioClip;
- public void init() {
- //html
- String filenames = getParameter("filelist");
- String fileList[] = filenames.split(" ");
- //下层panel
- b_play = new Button("Play");
- b_play.addActionListener(this);
- b_loop = new Button("Loop");
- b_loop.addActionListener(this);
- b_stop = new Button("Stop");
- b_stop.addActionListener(this);
- Panel panel_button = new Panel();
- panel_button.setLayout(new FlowLayout());
- panel_button.add(b_play);
- panel_button.add(b_loop);
- panel_button.add(b_stop);
- //中间panel
- ta = new TextArea();
- list = new List(5, false);
- list.addActionListener(this);
- for (int i = 0; i < fileList.length; i++) {
- list.add(fileList[i]);
- }
- Panel panel_text = new Panel();
- panel_text.setLayout(new GridLayout(1, 2));
- panel_text.add(list);
- panel_text.add(ta);
- //applet
- setLayout(new BorderLayout());
- add(panel_button, "South");
- add(panel_text, "Center");
- audioClip=getAudioClip(getCodeBase());
- }
- public void actionPerformed(ActionEvent e) {
- Object obj = e.getSource();
- if (obj == list) {
- String s = list.getSelectedItem();
- File file = new File(s);
- if (file.exists()) {
- audioClip=getAudioClip(getCodeBase(), s);
- ta.setText(s + " is opened successfully");
- audioClip.loop();
- }
- else {
- ta.setText(s + " doesn't exist, please choose another one");
- }
- }
- else if (obj == b_play) {
- audioClip.play();
- }
- else if (obj == b_loop) {
- audioClip.loop();
- }
- else if (obj == b_stop) {
- audioClip.stop();
- }
- }
- }