1,105,578 Community Members

Pizza GUI

Member Avatar
CorruptionInc
Newbie Poster
17 posts since Mar 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I have to create a Pizza GUI that shows pizza size choices and topping choices for pepperoni and mushrooms and then calculate the total. I'm having trouble constructing the GUI. Down in the createCenterPanel method Eclipse is tell me that radioButtonPanel, checkBoxPanel, and pricePanel cannot be resolved to a type.

import java.awt.*;

import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

public class PizzaPriceFrame extends JFrame {

	private JPanel pizzaPanel;
	private static final int FRAME_WIDTH = 300;
	private static final int FRAME_HEIGHT = 300;

	JRadioButton smallButton = new JRadioButton("Small");
	JRadioButton mediumButton = new JRadioButton("Medium");
	JRadioButton largeButton = new JRadioButton("Large");
	JCheckBox pepperoniButton = new JCheckBox("Pepperoni");
	JCheckBox mushroomsButton = new JCheckBox("Mushrooms");
	JTextField priceTextField = new JTextField();

	public PizzaPriceFrame() {
		setSize(FRAME_WIDTH, FRAME_HEIGHT);
		pizzaPanel = new JPanel();
		add(pizzaPanel, BorderLayout.CENTER);

		createRadioButtonPanel();
		createCheckBoxPanel();
		createCenterPanel();
		createPricePanel();
	}

	public void createRadioButtonPanel() {
	JPanel radioButtonPanel = new JPanel();
	radioButtonPanel.setLayout(new GridLayout(3, 1));
	radioButtonPanel.setBorder(new TitledBorder(new EtchedBorder(), "Size"));
	radioButtonPanel.add(smallButton);
	radioButtonPanel.add(mediumButton);
	radioButtonPanel.add(largeButton);
	}
	
	
	public void createCheckBoxPanel() {
	JPanel checkBoxPanel = new JPanel();
	checkBoxPanel.setLayout(new GridLayout(2, 1));
	checkBoxPanel.add(pepperoniButton);
	checkBoxPanel.add(mushroomsButton);
	}
	
	public void createPricePanel() {
	JPanel pricePanel = new JPanel(); // Uses FlowLayout
	pricePanel.add(new JLabel("Your Price:"));
	pricePanel.add(priceTextField);
	}
	
	public void createCenterPanel() {
	JPanel centerPanel = new JPanel(); // Uses FlowLayout
	centerPanel.add(radioButtonPanel);
	centerPanel.add(checkBoxPanel);
	
	
	// Frame uses BorderLayout by default
	add(centerPanel, BorderLayout.CENTER);
	add(pricePanel, BorderLayout.SOUTH);
	
	}
}

Any help is GREATLY appreciated.

Member Avatar
masijade
Industrious Poster
4,165 posts since Feb 2006
Reputation Points: 1,351 [?]
Q&As Helped to Solve: 510 [?]
Skill Endorsements: 22 [?]
Team Colleague
Featured
 
0
 

Those items are created locally in other methods meaning they are not available within another method. It lloks as though you want to use instance variables rather than local variables.

Member Avatar
CorruptionInc
Newbie Poster
17 posts since Mar 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

AHA, thank you very much.... that worked... now I'll work on the listener and calculations...

Member Avatar
CorruptionInc
Newbie Poster
17 posts since Mar 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Since I added the radio buttons to a buttongroup to make them mutually exclusive, now the radioButtonPanel doesn't show up...

import java.awt.*;

import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

public class PizzaPriceFrame extends JFrame {

	private JPanel pizzaPanel;
	private static final int FRAME_WIDTH = 300;
	private static final int FRAME_HEIGHT = 300;

	JRadioButton smallButton = new JRadioButton("Small");
	JRadioButton mediumButton = new JRadioButton("Medium");
	JRadioButton largeButton = new JRadioButton("Large");
	JCheckBox pepperoniButton = new JCheckBox("Pepperoni");
	JCheckBox mushroomsButton = new JCheckBox("Mushrooms");
	JTextField priceTextField = new JTextField();
	JPanel centerPanel = new JPanel();
	JPanel pricePanel = new JPanel();
	JPanel checkBoxPanel = new JPanel();
	JPanel radioButtonPanel = new JPanel();

	public PizzaPriceFrame() {
		setSize(FRAME_WIDTH, FRAME_HEIGHT);
		pizzaPanel = new JPanel();
		add(pizzaPanel, BorderLayout.CENTER);

		createRadioButtonPanel();
		createCheckBoxPanel();
		createCenterPanel();
		createPricePanel();
	}

	public void createRadioButtonPanel() {

		radioButtonPanel.setLayout(new GridLayout(3, 1));
		radioButtonPanel.setBorder(new TitledBorder(new EtchedBorder(), "Size"));
		ButtonGroup group = new ButtonGroup();
		group.add(smallButton);
		group.add(mediumButton);
		group.add(largeButton);
	}

	public void createCheckBoxPanel() {

		checkBoxPanel.setLayout(new GridLayout(2, 1));
		checkBoxPanel.add(pepperoniButton);
		checkBoxPanel.add(mushroomsButton);
	}

	public void createPricePanel() {
		// Uses FlowLayout
		pricePanel.add(new JLabel("Your Price:"));
		pricePanel.add(priceTextField);
	}

	public void createCenterPanel() {
		// Uses FlowLayout
		centerPanel.add(radioButtonPanel);
		centerPanel.add(checkBoxPanel);

		// Frame uses BorderLayout by default
		add(centerPanel, BorderLayout.NORTH);
		add(pricePanel, BorderLayout.SOUTH);

	}
}

What am I doing wrong?

Member Avatar
masijade
Industrious Poster
4,165 posts since Feb 2006
Reputation Points: 1,351 [?]
Q&As Helped to Solve: 510 [?]
Skill Endorsements: 22 [?]
Team Colleague
Featured
 
0
 

You haven't added the button group to anything.

Member Avatar
CorruptionInc
Newbie Poster
17 posts since Mar 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Is there a certain way I should do that? radioButtonPanel.add(group); doesn't work... says "The method add(Component) in the type Container is not applicable for the arguments (ButtonGroup)"

Member Avatar
masijade
Industrious Poster
4,165 posts since Feb 2006
Reputation Points: 1,351 [?]
Q&As Helped to Solve: 510 [?]
Skill Endorsements: 22 [?]
Team Colleague
Featured
 
0
 

You don't add the group as a single entity you add each of the buttons to the group and wherever it is to be displayed. The group simply ensures that only one of those radios are selected at any one time.

Member Avatar
CorruptionInc
Newbie Poster
17 posts since Mar 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Oh, I see....I didn't realize you had to add the buttons to BOTH the panel and the group... Horstmann didn't explain that... thanks.

Member Avatar
CorruptionInc
Newbie Poster
17 posts since Mar 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Darn! I'm almost there... Basically the program lets you choose the size of pizza, and your choice of two toppings, at an extra cost... then display the cost. I've got everything working except when you choose pepperoni AND mushrooms, the statement I tried didn't work.
Small – no extras $5.25
Medium – no extras $7.55
Large – no extras $9.35
Pepperoni – $1.25
Mushrooms – $1.10

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;

public class PizzaPriceFrame extends JFrame {

	private JPanel pizzaPanel;
	private static final int FRAME_WIDTH = 300;
	private static final int FRAME_HEIGHT = 300;

	JRadioButton smallButton = new JRadioButton("Small");
	JRadioButton mediumButton = new JRadioButton("Medium");
	JRadioButton largeButton = new JRadioButton("Large");
	JCheckBox peppCheckBox = new JCheckBox("Pepperoni");
	JCheckBox mushCheckBox = new JCheckBox("Mushrooms");
	JTextField priceTextField = new JTextField(7);
	JPanel centerPanel = new JPanel();
	JPanel pricePanel = new JPanel();
	JPanel checkBoxPanel = new JPanel();
	JPanel radioButtonPanel = new JPanel();
	private double price = 0;
	private double topPrice;
	private double showPrice;
	ActionListener listener = new PriceListener();

	public PizzaPriceFrame() {
		setSize(FRAME_WIDTH, FRAME_HEIGHT);
		pizzaPanel = new JPanel();
		add(pizzaPanel, BorderLayout.CENTER);

		createRadioButtonPanel();
		createCheckBoxPanel();
		createCenterPanel();
		createPricePanel();
	}

	public void createRadioButtonPanel() {

		radioButtonPanel.setLayout(new GridLayout(3, 1));
		radioButtonPanel
				.setBorder(new TitledBorder(new EtchedBorder(), "Size"));
		radioButtonPanel.add(smallButton);
		radioButtonPanel.add(mediumButton);
		radioButtonPanel.add(largeButton);
		ButtonGroup group = new ButtonGroup();
		group.add(smallButton);
		group.add(mediumButton);
		group.add(largeButton);
		smallButton.addActionListener(listener);
		mediumButton.addActionListener(listener);
		largeButton.addActionListener(listener);
	}

	public void createCheckBoxPanel() {

		checkBoxPanel.setLayout(new GridLayout(2, 1));
		checkBoxPanel.add(peppCheckBox);
		checkBoxPanel.add(mushCheckBox);
		peppCheckBox.addActionListener(listener);
		mushCheckBox.addActionListener(listener);

	}

	public void createPricePanel() {

		pricePanel.add(new JLabel("Your Price:"));
		pricePanel.add(priceTextField);
		priceTextField.setText("" + price + topPrice);
	}

	public void createCenterPanel() {

		centerPanel.add(radioButtonPanel);
		centerPanel.add(checkBoxPanel);

		add(centerPanel, BorderLayout.CENTER);
		add(pricePanel, BorderLayout.SOUTH);

	}

	class PriceListener implements ActionListener {
		public void actionPerformed(ActionEvent event) {
			if (smallButton.isSelected()) {
				price = 5.25;
				showPrice = price + topPrice;
				priceTextField.setText(" $" + showPrice);
				topPrice = 0;
			} else if (mediumButton.isSelected()) {
				price = 7.55;
				showPrice = price + topPrice;
				priceTextField.setText(" $" + showPrice);
				topPrice = 0;
			} else if (largeButton.isSelected()) {
				price = 9.35;
				showPrice = price + topPrice;
				priceTextField.setText(" $" + showPrice);
				topPrice = 0;
			}
			if (peppCheckBox.isSelected()) {
				topPrice = 1.25;
				showPrice = price + topPrice;
				priceTextField.setText(" $" + showPrice);
				topPrice = 0;
			} else if (mushCheckBox.isSelected()) {
				topPrice = 1.10;
				showPrice = price + topPrice;
				priceTextField.setText(" $" + showPrice);
				topPrice = 0;
			} else if (peppCheckBox.isSelected() && (mushCheckBox.isSelected())) {
				topPrice = 2.35;
				showPrice = price + topPrice;
				priceTextField.setText(" $" + showPrice);
			}
		}

	}
}

Tester

import javax.swing.JFrame;


public class PizzaPriceViewer {
	public static void main(String[] args)
	   {  
	      JFrame frame = new PizzaPriceFrame();
	      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	      frame.setTitle("Pizza Price GUI");
	      frame.setVisible(true);
	   }
	}
Member Avatar
mKorbel
Nearly a Posting Virtuoso
1,346 posts since Feb 2011
Reputation Points: 274 [?]
Q&As Helped to Solve: 277 [?]
Skill Endorsements: 14 [?]
 
0
 

there is bug with calculate Price (JCheckBox1 + JCheckBox2), check that ...

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;

public class PizzaPriceFrame extends JFrame {

    private static final long serialVersionUID = 1L;
    private JFrame frame;
    private JPanel pizzaPanel, centerPanel, pricePanel, checkBoxPanel, radioButtonPanel;
    private final int FRAME_WIDTH = 300;
    private final int FRAME_HEIGHT = 300;
    private ButtonGroup group;
    private JRadioButton smallButton, mediumButton, largeButton;
    private JCheckBox peppCheckBox, mushCheckBox;
    private JTextField priceTextField;
    private double price = 0.0;
    private double topPrice = 0.0;
    private double showPrice = 0.0;
    private ActionListener listener = new PriceListener();

    public PizzaPriceFrame() {
        pizzaPanel = new JPanel();
        pizzaPanel.setLayout(new BorderLayout(10, 10));

        createRadioButtonPanel();
        createCheckBoxPanel();
        createPricePanel();
        createCenterPanel();

        pizzaPanel.add(centerPanel, BorderLayout.CENTER);
        pizzaPanel.add(pricePanel, BorderLayout.SOUTH);

        frame = new JFrame("Pizza Price GUI");
        frame.add(pizzaPanel, 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);
    }

    private void createRadioButtonPanel() {
        radioButtonPanel = new JPanel();
        radioButtonPanel.setLayout(new GridLayout(3, 1));
        radioButtonPanel.setBorder(new TitledBorder(new EtchedBorder(), "Size"));

        group = new ButtonGroup();

        smallButton = new JRadioButton(" Small ");
        group.add(smallButton);
        smallButton.addActionListener(listener);
        radioButtonPanel.add(smallButton);

        mediumButton = new JRadioButton(" Medium ");
        group.add(mediumButton);
        mediumButton.addActionListener(listener);
        radioButtonPanel.add(mediumButton);

        largeButton = new JRadioButton(" Large ");
        group.add(largeButton);
        largeButton.addActionListener(listener);
        radioButtonPanel.add(largeButton);
    }

    private void createCheckBoxPanel() {
        checkBoxPanel = new JPanel();
        checkBoxPanel.setLayout(new GridLayout(2, 1));

        peppCheckBox = new JCheckBox(" Pepperoni ");
        peppCheckBox.addActionListener(listener);
        checkBoxPanel.add(peppCheckBox);

        mushCheckBox = new JCheckBox(" Mushrooms ");
        mushCheckBox.addActionListener(listener);
        checkBoxPanel.add(mushCheckBox);
    }

    private void createPricePanel() {
        pricePanel = new JPanel();
        pricePanel.add(new JLabel("Your Price:"));
        priceTextField = new JTextField(7);
        priceTextField.setFont(new Font("Serif", Font.BOLD, 12));
        priceTextField.setEditable(false);
        priceTextField.setForeground(Color.red);
        priceTextField.setBackground(pricePanel.getBackground());
        priceTextField.setDisabledTextColor(Color.red);
        priceTextField.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        pricePanel.add(priceTextField);
        priceTextField.setText(" n/a Price");
    }

    private void createCenterPanel() {
        centerPanel = new JPanel();
        centerPanel.add(radioButtonPanel);
        centerPanel.add(checkBoxPanel);
    }

    private class PriceListener implements ActionListener {

        public void actionPerformed(ActionEvent event) {
            topPrice = 0;
            if (smallButton.isSelected()) {
                price = 5.25;
            } else if (mediumButton.isSelected()) {
                price = 7.55;
            } else if (largeButton.isSelected()) {
                price = 9.35;
            }
            if (peppCheckBox.isSelected()) {
                topPrice = 1.25;
            } else if (mushCheckBox.isSelected()) {
                topPrice = 1.10;
            } else if (peppCheckBox.isSelected() && (mushCheckBox.isSelected())) {
                topPrice = 2.35;
            }
            EventQueue.invokeLater(new Runnable() {

                @Override
                public void run() {
                    showPrice = price + topPrice;
                    priceTextField.setText(" $" + showPrice);
                    System.out.println("Price dispayed");
                }
            });

        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                PizzaPriceFrame pPF = new PizzaPriceFrame();
                System.out.println("Run");
            }
        });
    }
}
Member Avatar
JamesCherrill
... trying to help
10,383 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

if (a) ...
else if (b) ...
else if (a && b) ... never executed because the else if won't be executed if (a==true) (see line 1) and (a && b) fails if (a==false)

Member Avatar
mKorbel
Nearly a Posting Virtuoso
1,346 posts since Feb 2011
Reputation Points: 274 [?]
Q&As Helped to Solve: 277 [?]
Skill Endorsements: 14 [?]
 
0
 

@ JamesCherrill

erggh bump, somebody did't know about that :-)

Member Avatar
CorruptionInc
Newbie Poster
17 posts since Mar 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks for the heads up... I fixed the problem by moving if a && b to the first if statement

Question Answered as of 3 Years Ago by masijade, mKorbel and JamesCherrill
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article