Java Applet: Basic Drum Set

蓝咒 提交于 2019-12-02 06:32:08

When you say "applet or GUI" I think you really mean applet or application--they are both GUIs. I'm not really familiar with AudioClip, but if it works as easy as it seems, then all you need to do is change your JApplet to a JPanel, then create a main method which creates a JFrame and set its content pane to your JPanel:

disclaimer: This code has not been tested and probably contains complilation errors (I will correct anything pointed out).

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();
}

If you cannot read the files, then I would suggest putting in the fully qualified path name rather than just "Snare.wav" (for example) which looks in the current CLASSPATH directory (which for eclipse, I believe is the project directory).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!