編輯:我解決了我的代碼,並設法讓歌曲在第一個單選按鈕上工作。但是現在如果我點擊按鈕,歌曲就會重新開始而不是停止。一旦按鈕被再次點擊,我該如何讓歌曲停止,我嘗試了一個null,但它不工作。爪哇Gui Radiobutton停止播放
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.AudioClip;
import java.net.URL;
public class RadioControls extends JPanel
{
private JRadioButton b1, b2, b3, b4, b5, b6;
private AudioClip[] music;
private AudioClip current;
private JSlider vSlider;
private JCheckBox airC;
//-----------------------------------------------------------------
// Sets up the GUI
//-----------------------------------------------------------------
public RadioControls()
{
URL url1, url2, url3, url4, url5, url6;
url1 = url2 = url3 = url4 = url5 = url6 = null;
// Obtain and store the audio clips to play
try
{
url1 = new URL ("file", "localhost", "westernBeat.wav");
url2 = new URL ("file", "localhost", "classical.wav");
url3 = new URL ("file", "localhost", "jeopardy.au");
url4 = new URL ("file", "localhost", "newAgeRythm.wav");
url5 = new URL ("file", "localhost", "eightiesJam.wav");
url6 = new URL ("file", "localhost", "hitchcock.wav");
}
catch (Exception exception) {}
music = new AudioClip[7];
music[0] = null; // Corresponds to "Make a Selection..."
music[1] = JApplet.newAudioClip (url1);
music[2] = JApplet.newAudioClip (url2);
music[3] = JApplet.newAudioClip (url3);
music[4] = JApplet.newAudioClip (url4);
music[5] = JApplet.newAudioClip (url5);
music[6] = JApplet.newAudioClip (url6);
b1 = new JRadioButton("1");
b1.setBackground(Color.yellow);
b2 = new JRadioButton("2");
b2.setBackground(Color.yellow);
b3 = new JRadioButton("3");
b3.setBackground(Color.yellow);
b4 = new JRadioButton("4");
b4.setBackground(Color.yellow);
b5 = new JRadioButton("5");
b5.setBackground(Color.yellow);
b6 = new JRadioButton("6");
b6.setBackground(Color.yellow);
//slider
vSlider = new JSlider(JSlider.HORIZONTAL, 1, 5, 1);
vSlider.setMinorTickSpacing(1);
vSlider.setMajorTickSpacing(1);
vSlider.setPaintTicks(true);
//air
airC = new JCheckBox("On/Off");
airC.setBackground(Color.cyan);
//add buttons
add (b1);
add (b2);
add (b3);
add (b4);
add (b5);
add (b6);
add (vSlider);
add (airC);
b1.addActionListener (new ButtonListener());
}
//*****************************************************************
// Represents the action listener for both control buttons.
//*****************************************************************
private class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
Object source = event.getSource();
if(source==b1)
music[1].play();
//I think the problem is here****//
if(source==null)
music[0].play();
}
}
}
我知道如何設置ActionListener我只是不明白我怎樣才能打電話給音樂文件播放 – user3350626
'JLayer':http://www.javazoom.net/javalayer/javalayer.html – nrubin29
我得到音樂播放,但現在我不能讓它停止,我會在一秒鐘內更新我的代碼。 – user3350626