|
| 1 | +### PizzaServingsCalculatorGUI |
| 2 | + |
| 3 | +#### Execution at the command line |
| 4 | + |
1 | 5 | ```
|
2 | 6 | javac PizzaServingsCalculator.java &&
|
3 | 7 | java PizzaServingsCalculator
|
4 | 8 | ```
|
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +#### `PizzaServingsCalculator` Class |
| 13 | + |
| 14 | +```java |
| 15 | +import java.awt.Color; |
| 16 | +import java.awt.event.ActionEvent; |
| 17 | +import java.awt.event.ActionListener; |
| 18 | +import java.awt.Font; |
| 19 | +import java.awt.GridLayout; |
| 20 | +import javax.swing.JButton; |
| 21 | +import javax.swing.JFrame; |
| 22 | +import javax.swing.JLabel; |
| 23 | +import javax.swing.JPanel; |
| 24 | +import javax.swing.JTextField; |
| 25 | +import javax.swing.SwingConstants; |
| 26 | + |
| 27 | +public class PizzaServingsCalculator extends JFrame { |
| 28 | + private final JButton button; |
| 29 | + private final JLabel label1; |
| 30 | + private final JLabel label2; |
| 31 | + private final JLabel label3; |
| 32 | + private final JPanel panel; |
| 33 | + private final JTextField textField; |
| 34 | + |
| 35 | + public PizzaServingsCalculator () { |
| 36 | + super("Pizza Servings Calculator"); |
| 37 | + setLayout(new GridLayout(4, 1, 0, 0)); |
| 38 | + |
| 39 | + label1 = new JLabel("Pizza Servings Calculator", SwingConstants.CENTER); |
| 40 | + label1.setForeground(Color.RED); |
| 41 | + label1.setFont(new Font(label1.getFont().getName(), Font.PLAIN, 18)); |
| 42 | + label2 = new JLabel("Enter the size of the pizza in inches:", SwingConstants.CENTER); |
| 43 | + label3 = new JLabel("", SwingConstants.CENTER); |
| 44 | + |
| 45 | + textField = new JTextField("", 4); |
| 46 | + textField.setEditable(true); |
| 47 | + |
| 48 | + ButtonHandler handler = new ButtonHandler(); |
| 49 | + button = new JButton("Calculate Servings"); |
| 50 | + button.addActionListener(handler); |
| 51 | + |
| 52 | + panel = new JPanel(); |
| 53 | + panel.add(label2); |
| 54 | + panel.add(textField); |
| 55 | + |
| 56 | + add(label1); |
| 57 | + add(panel); |
| 58 | + add(button); |
| 59 | + add(label3); |
| 60 | + } |
| 61 | + |
| 62 | + private class ButtonHandler implements ActionListener { |
| 63 | + @Override |
| 64 | + public void actionPerformed (ActionEvent e) { |
| 65 | + double size = Double.parseDouble(textField.getText()); |
| 66 | + double servings = Math.pow((size / 8), 2); |
| 67 | + label3.setText(String.format("A %.0f inch pizza will serve %.2f people.", size, servings)); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public static void main (String[] args) { |
| 72 | + PizzaServingsCalculator pizzaServingsCalculator = new PizzaServingsCalculator(); |
| 73 | + pizzaServingsCalculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 74 | + pizzaServingsCalculator.setSize(350, 300); |
| 75 | + pizzaServingsCalculator.setVisible(true); |
| 76 | + } |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +#### Requirements |
| 81 | + |
| 82 | +Write a GUI to calculate the number of servings that a circular pizza of a certain diameter will make. The GUI will have the following appearance: |
| 83 | + |
5 | 84 | 
|
| 85 | + |
| 86 | +It must include the following features: |
| 87 | +* the frame title must say 'Pizza Servings Calculator' |
| 88 | +* a grid layout will be used for the GUI |
| 89 | +* the `JLabel` title of the GUI will say 'Pizza Servings Calculator' and be in red and will be placed in grid slot 1 |
| 90 | +* a `JLabel` of 'Enter the size of the pizza in inches: ' will be placed in grid slot 2 followed by a `JTextField` where the pizza size will be entered and have a width of 4 |
| 91 | +* A `JButton` will be placed in grid slot 3 and will contain 'Calculate Servings' |
| 92 | +* a `JLabel`, initially blank, will be placed in grid slot 4 |
| 93 | + |
| 94 | +To execute the GUI, enter a pizza size in the `JTextField` and click the Calculate Servings button. The Calculate Servings button handler will then execute and calculate the number of servings and display it as shown in the following image: |
| 95 | + |
6 | 96 | 
|
| 97 | + |
| 98 | +The number of servings will be calculate using the following formula: `servings = (size / 8)**2` and displayed to two decimal places. You can use the `Double.parseDouble(textField.getText());` to get the string value from the `JTextField` and parse it to a double. This formula assumes that an 8 inch pie makes 1 serving. Based on the area of an 8" pie as one serving, the number of servings will vary with the ratio of the radius of the new pie to the 8" pie squared. Therefore, a 16" pie would give you a ratio of 16/8 or 2 squared which is 4 servings. Line 2 of the GUI contains two GUI components but each cell of a grid can only contain one component. This is where `JPanels` come in for GUI design. A `JPanel` is a container that simply holds other components, so we can use a `JPanel` as the component for line 2. We can create a `JPanel` by using a statement like `private JPanel line2 = new JPanel();`. Then we can add components to it with statements like `line2.add(variable that represents your JLabel for Enter the size of the pizza...);`. Then we can add line2 to the grid layout in slot 2. To set the layout of the frame to a 4 line grid layout, you would use a `setLayout` statement such as `setLayout(new GridLayout(4,1));`. Once the servings have been calculated, they are displayed in the `JLabel` in grid slot 4 as shown. Set the size of your GUI to (350, 300). This should give it the appearance as shown above. The class that represents the GUI should extend `JFrame`. Do not use the NetBeans GUI generator for this project. |
0 commit comments