⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 laughtrack.txt

📁 这个程序是用来模拟一个混合笑声的
💻 TXT
字号:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import javax.swing.*;

public class LaughTrack extends JFrame {

    public LaughTrack() {
        super("Laughtrack");
        setSize(190, 80);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container content = getContentPane();
        FlowLayout flo = new FlowLayout();
        content.setLayout(flo);
        LaughButton haha = new LaughButton();
        content.add(haha);
        setContentPane(content);
        setVisible(true);
    }

    public static void main(String[] arguments) {
        LaughTrack lt = new LaughTrack();
    }

}

class LaughButton extends JButton implements Runnable, ActionListener {
    AudioClip[] laugh = new AudioClip[4];
    Thread runner;

    LaughButton() {
        super("Start Laughing");
        addActionListener(this);
        for (int i = 0; i < laugh.length; i++) {
            try {
                URL laughIn = new URL("file:laugh" + i + ".wav");
                laugh[i] = JApplet.newAudioClip(laughIn);
            } catch (MalformedURLException e) { }
        }
    }

    public void actionPerformed(ActionEvent event) {
        String command = event.getActionCommand();
        if (command == "Start Laughing")
            startLaughing();
        if (command == "Stop Laughing")
            stopLaughing();
    }        

    void startLaughing() {
         if (runner == null) {
             runner = new Thread(this);
             runner.start();
             setText("Stop Laughing");
         }
    }

    void stopLaughing() {
        if (runner != null) {
            for (int i = 0; i < laugh.length; i++)
                if (laugh[i] != null)
                    laugh[i].stop();
            runner = null;
            setText("Start Laughing");
        }
    }

    public void run() {
        for (int i = 0; i < laugh.length; i++)
            if (laugh[i] != null)
                laugh[i].loop();
        Thread thisThread = Thread.currentThread();
        while (runner == thisThread) {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) { }
        }
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -