import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class PizzaCalculator
{
public static void main(String[] args)
{
JFrame frame = new PizzaFrame();
frame.setVisible(true);
}
}
class PizzaFrame extends JFrame
{
public PizzaFrame()
{
setTitle("Pizza Calculator");
setResizable(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new PizzaPanel();
this.add(panel);
this.pack();
centerWindow(this);
}
private void centerWindow(Window w)
{
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension d = tk.getScreenSize();
setLocation((d.width-w.getWidth())/2, (d.height-w.getHeight())/2);
}
}
class PizzaPanel extends JPanel implements ActionListener
{
private JRadioButton smallPizzaRadioButton,
mediumPizzaRadioButton,
largePizzaRadioButton,
clearRadioButton;
private JCheckBox sausageCheckBox,
pepperoniCheckBox,
salamiCheckBox,
hamburgerCheckBox,
olivesCheckBox,
mushroomsCheckBox,
peppersCheckBox,
onionsCheckBox;
private JTextField priceTextField;
private JLabel priceLabel;
private JButton calculateButton,
clearButton,
exitButton;
public PizzaPanel()
{
setLayout(new GridBagLayout());
Border loweredBorder = BorderFactory.createEtchedBorder();
// radio button panel
JPanel radioPanel = new JPanel();
ButtonGroup sizeGroup = new ButtonGroup();
radioPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
radioPanel.setBorder(BorderFactory.createTitledBorder(loweredBorder, "Size: "));
// small pizza radio button
smallPizzaRadioButton = new JRadioButton("Small Pizza");
smallPizzaRadioButton.addActionListener(this);
sizeGroup.add(smallPizzaRadioButton);
radioPanel.add(smallPizzaRadioButton);
// medium pizza radio button
mediumPizzaRadioButton = new JRadioButton("Medium Pizza");
mediumPizzaRadioButton.addActionListener(this);
sizeGroup.add(mediumPizzaRadioButton);
radioPanel.add(mediumPizzaRadioButton);
// large pizza radio button
largePizzaRadioButton = new JRadioButton("Large Pizza");
largePizzaRadioButton.addActionListener(this);
sizeGroup.add(largePizzaRadioButton);
radioPanel.add(largePizzaRadioButton);
// initialize radio button position
clearRadioButton = new JRadioButton("Clear", true);
clearRadioButton.addActionListener(this);
add(radioPanel, getConstraints(0,0,3,1, GridBagConstraints.CENTER));
/*************************************************************************************/
/* THIS IS WHERE I ATTEMPT TO CREATE A NEW GRID BAG LAYOUT INSIDE THE CURRENT GBL */
JPanel topPan = new JPanel();
ButtonGroup toppingsGroup = new ButtonGroup();
topPan.setLayout(new GridBagLayout());
topPan.setBorder(BorderFactory.createTitledBorder(loweredBorder, "Toppings "));
add(topPan, getConstraints(1,1,2,1, GridBagConstraints.CENTER));
// sausage check box
sausageCheckBox = new JCheckBox("Sausage");
toppingsGroup.add(sausageCheckBox);
topPan.add(sausageCheckBox);
add(sausageCheckBox, getConstraints(0,0,1,1, GridBagConstraints.WEST));
// pepperoni check box
pepperoniCheckBox = new JCheckBox("Pepperoni");
toppingsGroup.add(pepperoniCheckBox);
topPan.add(pepperoniCheckBox);
add(pepperoniCheckBox, getConstraints(0,1,1,1, GridBagConstraints.WEST));
// salami check box
salamiCheckBox = new JCheckBox("Salami");
toppingsGroup.add(salamiCheckBox);
topPan.add(salamiCheckBox);
add(salamiCheckBox, getConstraints(0,2,1,1, GridBagConstraints.WEST));
// hamburger check box
hamburgerCheckBox = new JCheckBox("Hamburger");
toppingsGroup.add(hamburgerCheckBox);
topPan.add(hamburgerCheckBox);
add(hamburgerCheckBox, getConstraints(0,3,1,1, GridBagConstraints.WEST));
// olives check box
olivesCheckBox = new JCheckBox("Olives");
toppingsGroup.add(olivesCheckBox);
topPan.add(olivesCheckBox);
add(olivesCheckBox, getConstraints(1,0,1,1, GridBagConstraints.WEST));
// mushrooms check box
mushroomsCheckBox = new JCheckBox("Mushrooms");
toppingsGroup.add(mushroomsCheckBox);
topPan.add(mushroomsCheckBox);
add(mushroomsCheckBox, getConstraints(1,1,1,1, GridBagConstraints.WEST));
// peppers check box
peppersCheckBox = new JCheckBox("Peppers");
toppingsGroup.add(mushroomsCheckBox);
topPan.add(mushroomsCheckBox);
add(peppersCheckBox, getConstraints(1,2,1,1, GridBagConstraints.WEST));
// onions check box
onionsCheckBox = new JCheckBox("Onions");
toppingsGroup.add(onionsCheckBox);
topPan.add(onionsCheckBox);
add(onionsCheckBox, getConstraints(1,3,1,1, GridBagConstraints.WEST));
/*************************************************************************/
// price label
priceLabel = new JLabel("Price:");
add(priceLabel, getConstraints(0,2,1,1, GridBagConstraints.EAST));
// price text field
priceTextField = new JTextField(10);
add(priceTextField, getConstraints(1,2,1,1, GridBagConstraints.WEST));
// calculate button
calculateButton = new JButton("Calculate");
calculateButton.addActionListener(this);
add(calculateButton, getConstraints(0,3,1,1, GridBagConstraints.CENTER));
// clear button
clearButton = new JButton("Clear");
clearButton.addActionListener(this);
add(clearButton, getConstraints(1,3,1,1, GridBagConstraints.CENTER));
// exit button
exitButton = new JButton("Clear");
exitButton.addActionListener(this);
add(exitButton, getConstraints(2,3,1,1, GridBagConstraints.CENTER));
}
// a method for setting grid bag constraints
private GridBagConstraints getConstraints(int gridx, int gridy, int gridwidth, int gridheight, int anchor)
{
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(5, 5, 5, 5);
c.ipadx = 0;
c.ipady = 0;
c.gridx = gridx;
c.gridy = gridy;
c.gridwidth = gridwidth;
c.gridheight = gridheight;
c.anchor = anchor;
return c;
}
public void actionPerformed(ActionEvent e)
{
}
}