I am required me to use RadioButtons and Checkboxes in my Java program so the user can easily select the options they would like to use (It is a "gas station").
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)
{
}
}
}
}
}
The error I am getting is at this part
RadioButtonHandler radioHand = new RadioButtonHandler ();
bronzeG.addItemListener (radioHand);
CheckBoxHandler checkHand = new CheckBoxHandler();
oilC.addItemListener (checkHand);
I do not understand why at bronzeG.addItemListener
, it says
addItemListener(java.awt.event.ItemListener) in javax.swing.AbstractButton cannot be applied to (New_Gas_Bar.RadioButtonHandler)
addItemListener
expects an ItemListener
to be passed as an argument. You are passing a class that extends ActionListener
Instead of
bronzeG.addItemListener(radioHand);
use
bronzeG.addActionListener(radioHand);
Read How to Write an Action Listener
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With