J2SE 照书抄的居然 不出声 求大大指点

安生 安生 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.applet.AudioClip;
import java.awt.Button;
import java.awt.Choice;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.*;
import java.net.URL;
import java.applet.Applet;
import java.io.File;
/**
*
* @author Shylock
*/
class Tool {

public static void showMsg(String msg)
{
System.out.println(msg);
}
}

class Audio extends Frame implements Runnable,ActionListener,ItemListener{
Thread thread;
Choice choice;
URL url;
AudioClip clip;

Button buttonPlay;
Button buttonLoop;
Button buttonStop;

String str;
Audio(){
thread = new Thread(this);
choice = new Choice();
choice.add("1.mid");
choice.add("1.mp3");

choice.addItemListener(this);
buttonPlay = new Button("Play");
buttonLoop = new Button("Loop");
buttonStop = new Button("Stop");

buttonPlay.addActionListener(this);
buttonLoop.addActionListener(this);
buttonStop.addActionListener(this);

setLayout(new FlowLayout());

add(choice);
add(buttonPlay);
add(buttonLoop);
add(buttonStop);

setSize(350,120);
setVisible(true);
addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
validate();
}

@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == buttonPlay){
clip.play();
}
else if(e.getSource() == buttonLoop){
clip.loop();
}
else{
clip.stop();
}
}

@Override
public void itemStateChanged(ItemEvent e) {
str = choice.getSelectedItem();
if(!thread.isAlive()){
thread = new Thread(this);
}
try{
thread.start();
}catch(Exception exp){
Tool.showMsg(exp.toString());
}
}

@Override
public void run() {
try{
File file = new File(str);
url = file.toURI().toURL();
Tool.showMsg(url.toString());
clip = Applet.newAudioClip(url);
Tool.showMsg(clip.toString());
Tool.showMsg(Thread.currentThread().getName()+"....run....");
}
catch(Exception e){
Tool.showMsg(e.toString());
}
}

}
public class AudioDemo {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Audio audio = new Audio();
//audio.thread.start();
}
}

...全文
评论 3
收藏 已收藏
分享
返回顶部
安生
Java SE 62,621 成员 307,251 内容