Java Applet:基本鼓组

时间:2014-05-10 00:14:48

标签: java audio applet javasound

我正在尝试编写一个有四个按钮的applet,所有按钮都播放一个简短的音频文件。目标是尝试让用户成功点击按钮任意次,从而创造一个节拍。这是我的尝试:

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;


public class drumKit extends JApplet
{

    private JButton snareButton;
    private JButton hiHatButton;
    private JButton bassButton;
    private JButton cymbalsButton;
    private AudioClip snare;
    private AudioClip hiHat;
    private AudioClip bass;
    private AudioClip cymbals;

    public void init()
    {
        setLayout (new FlowLayout());

        sampleButtons();

        snare = getAudioClip(getDocumentBase(), "Snare.wav");
        hiHat = getAudioClip(getDocumentBase(), "HiHat.wav");
        bass = getAudioClip(getDocumentBase(), "Kick.wav");
        cymbals = getAudioClip(getDocumentBase(), "Crash.wav");

    }

    private void sampleButtons()
    {
        snareButton = new JButton("Snare");
        hiHatButton = new JButton("Hi Hat");
        bassButton = new JButton("Kick");
        cymbalsButton = new JButton("Cymbals");

        snareButton.addActionListener(new ButtonListener());
        hiHatButton.addActionListener(new ButtonListener());
        bassButton.addActionListener(new ButtonListener());
        cymbalsButton.addActionListener(new ButtonListener());

        add(snareButton);
        add(hiHatButton);
        add(bassButton);
        add(cymbalsButton);
    }

    private class ButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            if (e.getSource() == snareButton)
                snare.play();
            if (e.getSource() == hiHatButton)
                hiHat.play();
            if (e.getSource() == bassButton)
                bass.play();
            if (e.getSource() == cymbalsButton)
                cymbals.play();



        }
    }
}

问题是,当我点击按钮时,什么都没有播放。我提到了列出的解决方案here,弹出一个窗口,阻止与applet的任何进一步交互。对不起,这里有点新手。 //谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

当你说“applet或GUI”时,我认为你真的是指applet或应用程序 - 它们都是两个 GUI。我对AudioClip并不熟悉,但如果它看起来很简单,那么您需要做的就是将JApplet更改为JPanel,然后创建一个主方法这会创建JFrame并将其内容窗格设置为JPanel

免责声明:此代码尚未经过测试,可能包含编辑错误(我会纠正任何指出的内容)。

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;


public class drumKit extends JPanel implements ActionListener
{

    private final JButton snareButton;
    private final JButton hiHatButton;
    private final JButton bassButton;
    private final JButton cymbalsButton;
    private final AudioClip snare;
    private final AudioClip hiHat;
    private final AudioClip bass;
    private final AudioClip cymbals;

    public drumKit()
    {
        super();

        // create buttons
        snareButton = new JButton("Snare");
        hiHatButton = new JButton("Hi Hat");
        bassButton = new JButton("Kick");
        cymbalsButton = new JButton("Cymbals");

        // setup audio clips
        snare = getAudioClip(getDocumentBase(), "Snare.wav");
        hiHat = getAudioClip(getDocumentBase(), "HiHat.wav");
        bass = getAudioClip(getDocumentBase(), "Kick.wav");
        cymbals = getAudioClip(getDocumentBase(), "Crash.wav");

        // set layout
        setLayout (new FlowLayout());

        // add this action listener to the buttons and add to this panel
        sampleButtons();
    }

    private void sampleButtons()
    {
        // add this as the each button's action listener
        snareButton.addActionListener(this);
        hiHatButton.addActionListener(this);
        bassButton.addActionListener(this);
        cymbalsButton.addActionListener(this);

        // add each button to this panel
        this.add(snareButton);
        this.add(hiHatButton);
        this.add(bassButton);
        this.add(cymbalsButton);
    }

    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource() == snareButton)
            snare.play();
        else if (e.getSource() == hiHatButton)
            hiHat.play();
        else if (e.getSource() == bassButton)
            bass.play();
        else if (e.getSource() == cymbalsButton)
            cymbals.play();
    }

    /**
     * main method creates a frame which contains this custom panel
     * and displays it.
     */
    public static void main(String ...args){
        // set the look and feel to the system's look and feel
        try{
            UIManager.setLookAndFeel(
                UIManager.getSystemLookAndFeelClassName());
        }catch(Exception e){
            // if this fails, who cares, the look and feel will be Java's
            // just continue
        }

        // create frame and make sure that when you close the frame the 
        //    program exits!
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // create your panel, set it to the frame's content pane, 
        //    then show the frame
        final JPanel panel = new drumKit();
        frame.setContentPane(panel);
        frame.setVisible(true);

        // resize the frame to be the preferred size of your panel
        frame.pack();
}

如果您无法读取文件,那么我建议输入完全限定的路径名​​,而不仅仅是"Snare.wav"(例如),它在当前的CLASSPATH目录中查找(对于eclipse,我相信是项目目录)。