/** Pizza GUI based on book pp. 498-501. Practice making radio buttons, * check boxes, text fields, etc. */ import java.text.*; // NumberFormat import java.awt.*; // GridLayout, etc. import java.awt.event.*; // ActionListener, etc. import javax.swing.*; // JFrame, JRadioButton, etc. import javax.swing.border.*; public class PizzaFrame extends JFrame { private JRadioButton smallButton; private JRadioButton mediumButton; private JRadioButton largeButton; private JCheckBox pepperoniBox; private JCheckBox anchoviesBox; private ActionListener listener; // needs to update price private JLabel priceArea; // doesn't need to be (big) text area /** We want our frame to consist of 3 basic parts: * A set of radio buttons for size of pizza * a couple check boxes to choose toppings (pepperoni and/or anchovies) * a section where the price of the pizza is to be shown. * So that this method doesn't get too long, these 3 parts will be * created in other methods. */ public PizzaFrame() { class DetectInput implements ActionListener { public void actionPerformed(ActionEvent e) { updatePrice(); } } listener = new DetectInput(); JPanel sizePanel = createButtons(); JPanel toppingPanel = createBoxes(); // put first 2 panels into another panel to group them JPanel centerPanel = new JPanel(); centerPanel.add(sizePanel); centerPanel.add(toppingPanel); JPanel pricePanel = createOutput(); getContentPane().add(centerPanel, BorderLayout.CENTER); getContentPane().add(pricePanel, BorderLayout.SOUTH); pack(); } public JPanel createButtons() { // make 3 buttons, and attach the listener to them; make medium default smallButton = new JRadioButton("Small"); mediumButton = new JRadioButton("Medium"); largeButton = new JRadioButton("Large"); smallButton.addActionListener(listener); mediumButton.addActionListener(listener); largeButton.addActionListener(listener); mediumButton.setSelected(true); // We need a button group because we want only 1 button to appear chosen // at a time. ButtonGroup group = new ButtonGroup(); group.add(smallButton); group.add(mediumButton); group.add(largeButton); // make a panel for the radio buttons JPanel radioButtonPanel = new JPanel(); radioButtonPanel.setLayout(new GridLayout(3,1)); // do we need a border? radioButtonPanel.add(smallButton); radioButtonPanel.add(mediumButton); radioButtonPanel.add(largeButton); return radioButtonPanel; } public JPanel createBoxes() { // make 2 buttons for the toppings boxes pepperoniBox = new JCheckBox("Pepperoni"); anchoviesBox = new JCheckBox("Anchovies"); pepperoniBox.addActionListener(listener); anchoviesBox.addActionListener(listener); JPanel boxPanel = new JPanel(); boxPanel.setLayout(new GridLayout(2,1)); boxPanel.add(pepperoniBox); boxPanel.add(anchoviesBox); // do we want border around panel? return boxPanel; } public JPanel createOutput() { JPanel pricePanel = new JPanel(); pricePanel.add(new JLabel("Your Price: ")); // initialize price and put it in panel too priceArea = new JLabel(); updatePrice(); // find price of default pizza pricePanel.add(priceArea); return pricePanel; } public void updatePrice() { double price; if (smallButton.isSelected()) price = 5.25; else if (mediumButton.isSelected()) price = 6.50; else // then it must be large price = 7.75; if (pepperoniBox.isSelected()) { price += 0.60; } if (anchoviesBox.isSelected()) { price += 0.30; } // format the price and put it in the priceArea label NumberFormat money = NumberFormat.getCurrencyInstance(); String output = money.format(price); priceArea.setText("" + output); repaint(); } }