Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
停止播放的Java Gui单选按钮_Java_User Interface_Actionlistener_Jradiobutton - Fatal编程技术网

停止播放的Java Gui单选按钮

停止播放的Java Gui单选按钮,java,user-interface,actionlistener,jradiobutton,Java,User Interface,Actionlistener,Jradiobutton,编辑:我修正了我的代码,并设法让歌曲在第一个单选按钮上工作。但是现在如果我松开按钮,歌曲将重新开始而不是停止。如何使歌曲停止再次单击按钮后,我尝试了null,但它不起作用 import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.applet.AudioClip; import java.net.URL; public class RadioControls extends JPanel { pr

编辑:我修正了我的代码,并设法让歌曲在第一个单选按钮上工作。但是现在如果我松开按钮,歌曲将重新开始而不是停止。如何使歌曲停止再次单击按钮后,我尝试了null,但它不起作用

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,如下所示:

JRadioButton myRadioButton = new JRadioButton("");
myRadioButton.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {
      // Do something here...
    }
});
public void actionPerformed (ActionEvent event)
    {
        Object source = event.getSource();
        if(source==b1){
             if(b1.isSelected()){
                 music[1].play();
             }
             else{
                 music[1].stop()
             }
        }

    }
要对复选框执行相同的操作,基本相同:

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(我认为 imageLabel.setVisible(假); 会那样做)然后把 imageLabel.setVisitble(真);
在actionListener中,问题是每次按钮上发生事件时,代码都会运行(无论是否选中)

您需要检查按钮状态,如下所示:

JRadioButton myRadioButton = new JRadioButton("");
myRadioButton.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {
      // Do something here...
    }
});
public void actionPerformed (ActionEvent event)
    {
        Object source = event.getSource();
        if(source==b1){
             if(b1.isSelected()){
                 music[1].play();
             }
             else{
                 music[1].stop()
             }
        }

    }

你也应该考虑用St.()停止歌曲,而不是指向一首空歌曲。

我知道如何设置ActListListor。我只是不明白我是如何得到它来调用音乐文件来播放的。我现在可以播放音乐,但是现在我不能让它停止,我将在几秒钟内更新我的代码。