我需要在我的 Java 程序中使用 RadioButtons 和 Checkboxes,以便用户可以轻松选择他们想要使用的选项(它是一个“加油站”)。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class New_Gas_Bar extends JFrame
{
public JPanel panel1,panel2,panel3,panel4,panel5;
public JLabel main1, main2, main3;
public JLabel gasBar,total;
public JButton button1,button2,button3,button4;
public JRadioButton bronzeG,silverG,goldG,selfS,fullS,fullService,selfService;
public JCheckBox oilC,windWash,windClean;
static double fullCost,selfCost;
public New_Gas_Bar()
{
super (" New Gas Bar");
setSize(640,640);
Container container = getContentPane();
panel1=new JPanel();
panel2=new JPanel();
panel1.setBackground(new Color(107,202,226));
panel1.setLayout(new GridLayout(7,1));
main1 = new JLabel (" Gas Bar Project ");
main2 = new JLabel (" ICS4U0 - September 2013");
main3 = new JLabel (" ");
button1 = new JButton ("Gas Station");
button2 = new JButton ("Car Wash");
button3 = new JButton ("Total");
button4 = new JButton ("Exit");
panel2.setBackground(new Color(144,160,170));
panel2.setLayout(new GridLayout(4,1));
panel2.add (button1);
panel2.add (button2);
panel2.add (button3);
panel2.add (button4);
panel1.add (main1);
panel1.add (main2);
//panel2.add(gasBar);
//panel3.add(total);
container.setLayout(new BorderLayout());
container.add(panel1,BorderLayout.NORTH);
container.add(panel2,BorderLayout.CENTER);
ButtonHandler handler = new ButtonHandler ();
button1.addActionListener (handler);
button2.addActionListener (handler);
button3.addActionListener (handler);
button4.addActionListener (handler);
ButtonGroup serveStyle = new ButtonGroup();
fullS = new JRadioButton ("Full Serve");
selfS = new JRadioButton ("Self Serve");
serveStyle.add (fullS);
serveStyle.add (selfS);
ButtonGroup serveGas = new ButtonGroup();
bronzeG = new JRadioButton("Bronze Service");
silverG = new JRadioButton("Silver Service");
goldG = new JRadioButton("Gold Service");
serveGas.add (bronzeG);
serveGas.add (silverG);
serveGas.add (goldG);
oilC = new JCheckBox("Oil Change");
windWash = new JCheckBox("Windshield Wash");
windClean = new JCheckBox("Windshield Cleaning");
RadioButtonHandler radioHand = new RadioButtonHandler ();
bronzeG.addItemListener (radioHand);
CheckBoxHandler checkHand = new CheckBoxHandler();
oilC.addItemListener (checkHand);
setVisible(true);
pack();
}
public static void main (String [] args)
{
New_Gas_Bar application = new New_Gas_Bar();
}
public class ButtonHandler implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
if (event.getSource () == button1)
{
panel1.setVisible(true);
panel2.setVisible(false);
panel3.setVisible(true);
panel4.setVisible(false);
panel5.setVisible(false);
}
else if (event.getSource () == button2)
{
}
else if (event.getSource () == button3)
{
}
else if (event.getSource () == button4)
{
System.exit(0);
}
}
}
public class RadioButtonHandler implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
if (event.getSource () == fullS)
{
}
else if (event.getSource () == selfS)
{
}
if (event.getSource () == bronzeG)
{
}
else if (event.getSource () == silverG)
{
}
else if (event.getSource () == goldG)
{
}
}
}
public class CheckBoxHandler implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
if (event.getSource () == checkBox1)
{
}
else if (event.getSource () == checkBox2)
{
}
else if (event.getSource () == checkBox3)
{
}
}
}
}
}
我得到的错误是在这部分
RadioButtonHandler radioHand = new RadioButtonHandler ();
bronzeG.addItemListener (radioHand);
CheckBoxHandler checkHand = new CheckBoxHandler();
oilC.addItemListener (checkHand);
我不明白为什么bronzeG.addItemListener
,它说
addItemListener(java.awt.event.ItemListener) in javax.swing.AbstractButton cannot be applied to (New_Gas_Bar.RadioButtonHandler)