Question: Need help in Java. I have the following program and once I've selected one or more pizza toppings, I cannot go back to plain, without

Need help in Java. I have the following program and once I've selected one or more pizza toppings, I cannot go back to plain, without first unchecking all other selections. I would like to be able to click back on plain and have all the other checkboxes clear. Also, plain and small must be selected initially by default. I also want plain to be unchecked when the user selects some other topping. Here's the code:

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;

import javax.swing.border.EtchedBorder;

import javax.swing.border.TitledBorder;

//main class that extends the Jframe class

public class PizzaPrice extends JFrame {

// required initialisations

private static final long serialVersionUID = 1L;

private JFrame frame;

private JPanel pPanel, centerPanel, pricePanel, checkBoxPanel, rBPanel;

private final int FRAME_WIDTH = 300;

private final int FRAME_HEIGHT = 300;

private ButtonGroup grp;

private JRadioButton smallButton, mediumButton, largeButton;

private JCheckBox peppCheckBox, mushCheckBox, plainCheckBox,

sausCheckBox;

private JTextField priceTextField;

private double price = 0.00;

private double topPrice = 0.00;

private double showPrice = 0.00;

private ActionListener listener = new PriceListener();

// method to calculate the prices of a pizza

public PizzaPrice() {

pPanel = new JPanel();

pPanel.setLayout(new BorderLayout(10, 10));

ButtonPanel();

checkBoxPanel();

PricePanel();

CenterPanel();

pPanel.add(centerPanel, BorderLayout.CENTER);

pPanel.add(pricePanel, BorderLayout.SOUTH);

frame = new JFrame("Welcome to Tamer's Pizzeria");

frame.add(pPanel, BorderLayout.CENTER);

frame.setPreferredSize(new Dimension(FRAME_WIDTH, FRAME_HEIGHT));

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.pack();

frame.setLocation(100, 100);

frame.setVisible(true);

}

// method to create a button for the pizzas

private void ButtonPanel() {

rBPanel = new JPanel();

rBPanel.setLayout(new GridLayout(1, 3));

rBPanel.setBorder(new TitledBorder(new EtchedBorder(), "Pizza Size"));

grp = new ButtonGroup();

smallButton = new JRadioButton(" Small ", true);

grp.add(smallButton); // adding them to the grp

smallButton.addActionListener(listener);

rBPanel.add(smallButton);

mediumButton = new JRadioButton(" Medium ", false);

grp.add(mediumButton);

mediumButton.addActionListener(listener);

rBPanel.add(mediumButton);

largeButton = new JRadioButton(" Large ", false);

grp.add(largeButton);

largeButton.addActionListener(listener);

rBPanel.add(largeButton);

}

// method to create a checkbox for the pizza toppings

private void checkBoxPanel() {

checkBoxPanel = new JPanel();

checkBoxPanel.setLayout(new GridLayout(2, 3));

checkBoxPanel.setBorder(new TitledBorder(new EtchedBorder(),

"Toppings"));

plainCheckBox = new JCheckBox(" Plain ", true);

plainCheckBox.addActionListener(listener);

checkBoxPanel.add(plainCheckBox);

sausCheckBox = new JCheckBox(" Sausage ", false);

sausCheckBox.addActionListener(listener);

checkBoxPanel.add(sausCheckBox);

mushCheckBox = new JCheckBox(" Mushrooms ", false);

mushCheckBox.addActionListener(listener);

checkBoxPanel.add(mushCheckBox);

peppCheckBox = new JCheckBox(" Pepperoni ", false);

peppCheckBox.addActionListener(listener);

checkBoxPanel.add(peppCheckBox);

}

//method to display price

private void PricePanel() {

pricePanel = new JPanel();

pricePanel.add(new JLabel("Price Total : "));

priceTextField = new JTextField(7);

priceTextField.setFont(new Font("Serif", Font.BOLD, 12));

priceTextField.setForeground(Color.red);

priceTextField.setBackground(pricePanel.getBackground());

priceTextField.setDisabledTextColor(Color.red);

priceTextField.setHorizontalAlignment

(javax.swing.SwingConstants.CENTER);

pricePanel.add(priceTextField);

priceTextField.setText("5.00");

}

private void CenterPanel() {

centerPanel = new JPanel();

centerPanel.add(rBPanel);

centerPanel.add(checkBoxPanel);

}

private class PriceListener implements ActionListener {

public void actionPerformed(ActionEvent event) {

topPrice = 0;

if (smallButton.isSelected()) {

price = 5.00;

} else if (mediumButton.isSelected()) {

price = 8.00;

} else if (largeButton.isSelected()) {

price = 11.50;

}

if (peppCheckBox.isSelected() && smallButton.isSelected()) {

topPrice += .50;

plainCheckBox.setSelected(false);

}

if (sausCheckBox.isSelected() && smallButton.isSelected()) {

topPrice += .50;

plainCheckBox.setSelected(false);

}

if (mushCheckBox.isSelected() && smallButton.isSelected()) {

topPrice += .50;

plainCheckBox.setSelected(false);

}

if (plainCheckBox.isSelected() && smallButton.isSelected()) {

topPrice += 0;

plainCheckBox.setSelected(false);

}

if (peppCheckBox.isSelected() && mediumButton.isSelected()) {

topPrice += 1.00;

plainCheckBox.setSelected(false);

}

if (sausCheckBox.isSelected() && mediumButton.isSelected()) {

topPrice += 1.00;

plainCheckBox.setSelected(false);

}

if (mushCheckBox.isSelected() && mediumButton.isSelected()) {

topPrice += 1.00;

plainCheckBox.setSelected(false);

}

if (plainCheckBox.isSelected() && mediumButton.isSelected()) {

topPrice += 0;

}

if (peppCheckBox.isSelected() && largeButton.isSelected()) {

topPrice += 1.75;

plainCheckBox.setSelected(false);

}

if (sausCheckBox.isSelected() && largeButton.isSelected()) {

topPrice += 1.75;

plainCheckBox.setSelected(false);

}

if (mushCheckBox.isSelected() && largeButton.isSelected()) {

topPrice += 1.75;

plainCheckBox.setSelected(false);

}

if (plainCheckBox.isSelected() && largeButton.isSelected()) {

topPrice += 0;

}

EventQueue.invokeLater(new Runnable() {

@Override

public void run() {

showPrice = price + topPrice;

priceTextField.setText(" $" + showPrice);

System.out.println("Price is dispayed"); //for testing

}

});

}

}

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

@Override

public void run() {

// calling the class to run the main method

PizzaPrice pp = new PizzaPrice();

System.out.println("Calculator Running");

}

});

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!