I can't seem to get my radio buttons to work. My program basically is a menu. My problem is when i select a burger from my radio buttons and then click calcuate it says my subtotal is 200(which I set as default). But since i clicked a button it should be 4.95,5.95,or 6.95. What should I do? Any help is appreciated.
import javax.swing.*; import java.awt.*; import java.awt.event.*; import javax.swing.JRadioButton; import javax.swing.ButtonGroup; public class DisplayMenu extends JFrame { private static JPanel panel; private static JPanel panel2; private static JPanel panel3; private static JPanel orderPanel; private static JPanel buttonPanel; private JLabel message1; private JLabel message2; private JLabel message3; private JTextField text1; private JTextField text2; private JTextField text3; private JButton calcButton; private JButton exitButton; private JRadioButton jrb1; private JRadioButton jrb2; private JRadioButton jrb3; private JCheckBox Jchk; private JCheckBox Jchk2; private JCheckBox Jchk3; private JCheckBox Jchk4; private JCheckBox Jchk5; double BurgerTotal; double cond1; double cond2; double cond3; double cond4; double cond5; double cond6; double Subtotal; public static void main(String[] args) { // TODO Auto-generated method stub DisplayMenu frame = new DisplayMenu(); frame.setTitle ("Hamburger Hamlet Order"); frame.setSize (400, 400); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.buildPanel(); frame.add(panel3); frame.setVisible(true); } private void buildPanel() { panel = new JPanel(); jrb1 = new JRadioButton ("burger1(4.95)"); jrb2 = new JRadioButton ("burger2(5.95)"); jrb3 = new JRadioButton ("burger3(6.95)"); Jchk = new JCheckBox("lettuce"); Jchk2 = new JCheckBox("onion"); Jchk3 = new JCheckBox("tomatoes"); Jchk4 = new JCheckBox("nug"); Jchk5 = new JCheckBox("byug"); ButtonGroup group = new ButtonGroup(); group.add(jrb1); group.add(jrb2); group.add(jrb3); if (Jchk.isSelected()){ cond1 = .25; } else{ cond1= 0; } if (Jchk2.isSelected()){ cond2 = .25; } else{ cond2 = 0; } if (Jchk3.isSelected()){ cond3 = .25; } else{ cond3= 0; } if (Jchk4.isSelected()){ cond4 = .25; } else{ cond4 = 0; } if (Jchk5.isSelected()){ cond5 = .25; } else{ cond5 = 0; } add(panel, BorderLayout.WEST); //add the left panel to the border layout panel.setLayout (new BoxLayout (panel, BoxLayout.Y_AXIS)); //set the layout of left to box layout //display the buttons on the panel calcButton = new JButton ("calculate"); exitButton = new JButton ("Exit"); panel.add (jrb1); panel.add (jrb2); panel.add (jrb3); if (jrb1.isSelected()){ BurgerTotal = 4.95; } else if (jrb2.isSelected()){ BurgerTotal = 5.95; } else if (jrb3.isSelected()){ BurgerTotal = 6.95; } else{ BurgerTotal =200; } calcButton.addActionListener(new CalcButtonListener()); exitButton.addActionListener(new ExitButtonListener()); panel2 = new JPanel(); panel2.setLayout(new GridLayout(3,2)); panel2.add (Jchk); panel2.add (Jchk2); panel2.add (Jchk3); panel2.add (Jchk4); panel2.add (Jchk5); add(panel2, BorderLayout.WEST); //add the left panel to the border layout panel2.setLayout (new BoxLayout (panel2, BoxLayout.Y_AXIS)); //set the layout of left to box layout orderPanel = new JPanel(); orderPanel.setLayout(new GridLayout(3,2)); message3 = new JLabel ("Subtotal: "); orderPanel.add(message3); text3 = new JTextField(10); text3.setEditable(false); orderPanel.add(text3); message2 = new JLabel ("Tax(7.85%): "); orderPanel.add(message2); text2 = new JTextField(10); text2.setEditable(false); orderPanel.add(text2); message1 = new JLabel ("Total due: "); text1 = new JTextField(10); text1.setEditable(false); orderPanel.add(message1); orderPanel.add(text1); buttonPanel = new JPanel(); buttonPanel.setLayout(new GridLayout(3,1)); buttonPanel.add(calcButton); buttonPanel.add(exitButton); panel3 = new JPanel(); panel3.setLayout(new GridLayout(2,2)); panel3.add(panel); panel3.add(panel2); panel3.add(orderPanel); panel3.add(buttonPanel); } private class CalcButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { double subtotal; subtotal = BurgerTotal; text3.setText(Double.toString(subtotal)); } } //exits when you click exit private class ExitButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { System.exit(ABORT); } } }
--- Update ---
ok i figured that part out I just moved my if statements to the actionlistener