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.

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.

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

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?

You haven't added the button group to anything.

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)"

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.

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.

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);
	   }
	}

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");
            }
        });
    }
}

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)

@ JamesCherrill

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.