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 :(得分: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
中答案 1 :(得分:0)
问题是每次按钮发生事件时都会运行代码(无论是选中还是取消选中)
您需要检查按钮状态,如下所示:
public void actionPerformed (ActionEvent event)
{
Object source = event.getSource();
if(source==b1){
if(b1.isSelected()){
music[1].play();
}
else{
music[1].stop()
}
}
}
您还应该考虑使用.stop()停止歌曲,而不是指向一首空歌曲。