Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a frame with components for an order form for a pizza (see image of frame)

ID: 3689586 • Letter: W

Question

Write a frame with components for an order form for a pizza (see image of frame). The frame should be able to run itself and include an appropriate title bar. All your frame components will have an appropriate pizza colored background and foreground. The purpose of this exercise is to show layouts and panels with different kinds of events. You will use a border layout with panels to divide the frame into 3 sections - top, middle, and bottom. All of the sections will use the panels with a grid layout for the multiple components.

The top section will have the centered title telling the name of the company (in a larger and different style typeface) and underneath it separate centered instructions to choose your pizza size and toppings.

The middle section will have four radio buttons in a button group: small - $7, medium - $9, large - $11, and extra large - $13. The right side of the middle section will also have a list object for the available toppings - Eggplant, Green Peppers, Hot Peppers, Pepperoni, Sausage, Mushrooms, and Anchovies (of which they can pick as many as they want from 0 - 7). Each topping will add $1 to the total price. Before they pick any pizza size or toppings, there should be no size or toppings selected.

The bottom section will have first a label saying APrice of your pizza is $0.00", and then to the right of the price label will be a checkbox promoting a $5.00 side order of hot wings. The price of the pizza will be updated as they pick different sizes or toppings, also in a larger point size and different typeface with the price formatted in Currency.

Use the setSize() for the frame, but be careful of the sizing, components tend to fill up unused areas in a border layout. For the list, make sure you can see all items but don't let there be any extra space, just resize the frame so everything fits and there is no extra space and don=t let the user change the frame=s size on you.

As soon as the user clicks on a new size, topping, or the wings check box, update the price immediately. Let the user make changes to the form as often as they want, seeing the updated price immediately. Have a price variable for the size price (which could include the extra wings price), a price variable for the topping price, and a total price. Only calculate and update what they change (ex. if they add a topping, the size does not have to be recalculated).

When they pick a size radio button, check to see which one they picked and change the size price (you will be an ItemListener). Then check to see if the wings check box is checked off, if so then add the $5.00 to the size price. Add to this size price the topping price, format the final price to Currency and update the price label. When they pick a topping or toppings (hold control key down for multiple toppings), calculate the topping price (add $1.00 for each topping they picked) (you will be a ListSelectionListener). Add to this the size price, format the final price to Currency, and update the price label.   

1st Run:

Large Pizza with Eggplant and Hot Peppers, with a side order of wings.

2nd Run:

Medium Pizza with Green Peppers, Pepperoni, and Mushrooms, without a side order of wings.

Save the frame class code with documentation - full 4-5 line paragraph describing the frame and at least 5 documentation lines throughout the code. Run with each of the two runs, each time saving a picture of the actual frame results (they can be saved together in the same file as a .jpg). You can copy them with <alt><ptrscr> (just active frame) and paste both pictures into Paint for saving as a .jpg file. Submit both files (.java java code and .jpg output image of the two frame images).

Explanation / Answer

mport java.awt.BorderLayout;
import java.awt.Container;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;

/**
* Creates a form that allows someone to order a pizza.
* This version displays Swing components that are organized
* reasonably but does not do anything with the
* user input.
*
* @author Barbara Lerner
* @version February 3, 2010
*
*/
public class PizzaOrder {
   /**
   * Creates the panel that holds the buttons to place or cancel the order
   * @return the button panel
   */
   private JPanel createButtonPanel() {
       JPanel buttonPanel = new JPanel();
       buttonPanel.add (new JButton("Place order"));
       buttonPanel.add (new JButton("Cancel order"));
       return buttonPanel;
   }

   /**
   * Creates the panel with the temperature slider
   * @return the temperature panel
   */
   private JPanel createTemperaturePanel() {
       JPanel temperaturePanel = new JPanel();
       temperaturePanel.add (new JLabel ("Temperature"));
       temperaturePanel.add (new JSlider());
       return temperaturePanel;
   }

   /**
   * Creates the menu of toppings
   * @return the menu of toppings
   */
   private JComboBox createToppingsMenu() {
       JComboBox comboBox = new JComboBox();
       comboBox.addItem("Green peppers");
       comboBox.addItem("Hot Peppers");
       comboBox.addItem("Pepperoni");
       comboBox.addItem("Sausage");
       comboBox.addItem("Mushrooms");
       comboBox.addItem("Anchovies");
       return comboBox;
   }

   /**
   * Creates the panel where the name of the person placing the order is entered.
   * @return the name panel
   */
   private JPanel createNamePanel() {
       JPanel namePanel = new JPanel();
       namePanel.add (new JLabel("Name: "));
       namePanel.add (new JTextField(15));
       return namePanel;
   }

   /**
   * Creates the panel to select delivery
   * @return the delivery panel
   */
   private JPanel createDeliveryPanel() {
       JPanel deliveryPanel = new JPanel();
       deliveryPanel.add (new JCheckBox());
       deliveryPanel.add (new JLabel("Delivery"));
       return deliveryPanel;
   }

   /**
   * A program to display a pizza order form.
   * @param args None expected.
   */
public PizzaOrder()
{
/*
* Pizza size specified by radio buttons
*/
// Create radio buttons, and put them in a button group.
small = new JRadioButton("Small");
small.setActionCommand("Small");
medium = new JRadioButton("Medium");
medium.setActionCommand("Medium");
large = new JRadioButton("Large");
large.setActionCommand("Large");
ButtonGroup sizeGroup = new ButtonGroup();
sizeGroup.add(small);
sizeGroup.add(medium);
sizeGroup.add(large);

// Create a size listener and register it in all size buttons.
SizeListener sl = new SizeListener();
small.addActionListener(sl);
medium.addActionListener(sl);
large.addActionListener(sl);

JPanel p1 = new JPanel();
p1.add(small);
p1.add(medium);
p1.add(large);
p1.setBorder(new TitledBorder(new EtchedBorder(), "Size"));


// Create a topping item listener and register it in both check boxes.
ToppingListener tl = new ToppingListener();
pepperoni.addItemListener(tl);
anchovies.addItemListener(tl);

JPanel p2 = new JPanel();
  
p2.setBorder(new TitledBorder(new EtchedBorder(), "Toppings"));

Container pane = getContentPane();
pane.setLayout(new FlowLayout());

/*
* Delivery/eat-in option by a combo box
*/
// Create a combo box, whose choices are specified in an aray of strings.
inOrOut = new JComboBox(inOrOutChoices);
inOrOut.setEditable(false);

// Create a delivery listener object and register it.
inOrOut.addActionListener(new DeliveryListener());

JPanel p3 = new JPanel();
p3.add(inOrOut);
p3.setBorder(new TitledBorder(new EtchedBorder(), "Delivery"));

// Finally add all panels to the content pane and set its size.
pane.add(p1);
pane.add(p2);
pane.add(p3);
pane.add(message);
setSize(250, 300);
}

   public static void main(String[] args) {
       JFrame f = new JFrame();
       f.setTitle("Pizza Order");
      
       // Create the panel that will display the image and text
       PizzaOrder order = new PizzaOrder();
       Container contentPane = f.getContentPane();
      
       // Delivery
       JPanel deliveryPanel = order.createDeliveryPanel();
       contentPane.add (deliveryPanel, BorderLayout.NORTH);
      
       // Person ordering
       JPanel namePanel = order.createNamePanel();
       contentPane.add(namePanel, BorderLayout.SOUTH);
      
       // Toppings
       JComboBox comboBox = order.createToppingsMenu();
       contentPane.add(comboBox, BorderLayout.CENTER);
      
      
      
       // Order and cancel buttons
       JPanel buttonPanel = order.createButtonPanel();
       contentPane.add(buttonPanel, BorderLayout.EAST);
      
       // Make the window the minimum size that displays all
       // components.
       f.pack();
      
       // Display the window.
       f.setVisible(true);

   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote