2014-04-01 51 views
0

編輯:我解決了我的代碼,並設法讓歌曲在第一個單選按鈕上工作。但是現在如果我點擊按鈕,歌曲就會重新開始而不是停止。一旦按鈕被再次點擊,我該如何讓歌曲停止,我嘗試了一個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(); 
    } 
    } 
} 

回答

0

的問題是你的代碼運行每次按鈕時發生的事件(無論選中或清除)您需要檢查這樣的按鈕狀態

public void actionPerformed (ActionEvent event) 
    { 
     Object source = event.getSource(); 
     if(source==b1){ 
      if(b1.isSelected()){ 
       music[1].play(); 
      } 
      else{ 
       music[1].stop() 
      } 
     } 

    } 

你應該還要考慮用.stop()停止歌曲,而不是指向空歌。

0

你有一個ActionListener添加到按鈕這樣的:

JRadioButton myRadioButton = new JRadioButton(""); 
myRadioButton.addActionListener(new ActionListener(){ 
    public void actionPerformed(ActionEvent e) { 
     // Do something here... 
    } 
}); 

爲了做同樣的一個複選框,這幾乎是相同的:

JCheckBox b = new JCheckBox("JCheckBox"); 
     b.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       //change Image here 
      } 
     }); 

要顯示的圖像,你可以做很多事情。其中之一是:

ImageIcon icon = new ImageIcon(imgURL); 
JLabel imageLabel = new JLabel(); 
imageLabel.setIcon(icon); 

你可以隱藏imageLabel的OnStart(我認爲 imageLabel.setVisible(假); 會做到這一點),然後把 imageLabel.setVisitble(真); 在你的ActionListener

+0

我知道如何設置ActionListener我只是不明白我怎樣才能打電話給音樂文件播放 – user3350626

+0

'JLayer':http://www.javazoom.net/javalayer/javalayer.html – nrubin29

+0

我得到音樂播放,但現在我不能讓它停止,我會在一秒鐘內更新我的代碼。 – user3350626