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

📄 soundtest.java

📁 Java程序设计技巧与开发实例附书源代码。
💻 JAVA
字号:

import java.net.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.*;

public class SoundTest
        extends JFrame
        implements ActionListener
{
    private JButton playMusic = new JButton("Play Music");
    private JButton loopMusic = new JButton("Loop Music");
    private JButton stopMusic = new JButton("Stop Music");
    private JButton playSound = new JButton("Play Sound");
    private JButton stopSound = new JButton("Stop Sound");
    private AudioClip music = null, sound = null;

    public SoundTest()
    {
        super("Sound Test");
        try
        {
            String separator = System.getProperty("file.separator");
            String preface = "file:" + System.getProperty("user.dir")
                    + separator + "Audio" + separator;
            music = Applet.newAudioClip(new URL(preface + "music.wav"));
            sound = Applet.newAudioClip(new URL(preface + "applause.wav"));
        }
        catch (MalformedURLException murle)
        {
            System.err.println("Error loading files: " + murle);
        }
        Container content = getContentPane();
        content.setLayout(new FlowLayout());
        content.add(playMusic);
        playMusic.addActionListener(this);
        content.add(loopMusic);
        loopMusic.addActionListener(this);
        content.add(stopMusic);
        stopMusic.addActionListener(this);
        content.add(playSound);
        playSound.addActionListener(this);
        content.add(stopSound);
        stopSound.addActionListener(this);
        validate();
        pack();
        setVisible(true);
    }

    public void actionPerformed(ActionEvent ae)
    {
        if (ae.getSource() == playMusic)
        {
            music.play();
        }
        else if (ae.getSource() == loopMusic)
        {
            music.loop();
        }
        else if (ae.getSource() == stopMusic)
        {
            music.stop();
        }
        else if (ae.getSource() == playSound)
        {
            sound.play();
        }
        else if (ae.getSource() == stopSound)
        {
            sound.stop();
        }
    }

    public static void main(String args[])
    {
        SoundTest st = new SoundTest();
    }
}

⌨️ 快捷键说明

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