0
你好,我試圖做一個java鼓套件,但我的代碼不播放聲音,當我點擊按鈕,我不知道爲什麼。我有代碼的文件中的聲音文件,所以我知道這不是問題。這裏是我的代碼...爪哇鼓組項目
import java.applet.AudioClip;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
public class FinalProjectst extends JApplet implements ActionListener {
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(getCodeBase(), "Snare.wav");
hiHat = getAudioClip(getCodeBase(), "HiHat.wav");
bass = getAudioClip(getCodeBase(), "Kick.wav");
cymbals = getAudioClip(getCodeBase(), "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);
}
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();
}
}
「ButtonListener」的定義在哪裏? – copeg
嘗試使用'snareButton.addActionListener(this);'和其他按鈕相同。 –