Java小程序:基本架子鼓 [英] Java Applet: Basic Drum Set
问题描述
我想能编写有四个按钮,所有这一切都起到短暂的音频文件的小程序。的目标是尝试有用户成功单击按钮任何次数,因而产生了一拍。这里是我的尝试:
I am trying to program an applet that has four buttons, all of which play a short audio file. The goal is to try and have the user successfully click the buttons any number of times, therefore creating a beat. Here is my attempt:
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();
}
}
}
问题是,当我点击的按钮,没有什么发挥。我提到所列方案的此处,会弹出一个窗口preventing与applet任何进一步的交互。对不起,有点这里新手。
//感谢您的帮助。
The problem is, when I click the buttons, nothing plays. I referred to the solutions listed here, a window pops up preventing any further interactions with the applet. Sorry, a bit of a newbie here. //Thanks for your help.
推荐答案
当你说applet或GUI我觉得你真的是小程序或应用程序 - 它们的两个的图形用户界面。我不是很熟悉的音频剪辑
,但如果它的工作原理,因为它似乎很容易,那么所有你需要做的是改变你的 JApplet的
到的JPanel
,然后创建它创建了一个的JFrame
main方法,并设置其内容窗格你的的JPanel
:
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
:
声明:此code尚未经过测试,并可能包含complilation错误(我会纠正任何人士指出)
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();
}
如果您无法读取文件,那么我会建议把在完全合格的路径名,而不仅仅是Snare.wav
(例如)看起来在当前CLASSPATH目录(Eclipse的,我相信是项目目录)。
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).
这篇关于Java小程序:基本架子鼓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!