Help would be so much appreciated new Irish Java programmer
Hi
I am a beginner java programmer and at the moment, I am learning about the Swing package. I was just wondering if anyone could help me with the code. When I compile it, I get the following 2 errors in cmd:
b1 is already defined in pizza()
and
setBorder in javax swing cannot be applied to pizza ButtonListener
Thank you so much if you can be of any help,
Joan
Code java:
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
public class pizza extends JFrame
{
public static void main(String [] args)
{
new pizza();
}
private JButton buttonOK;
private JRadioButton small, medium, large;
private JCheckBox pepperoni, mushrooms, anchovies;
public pizza()
{
this.setSize(320,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Order your pizza ");
ButtonListener b1 = new ButtonListener();
JPanel mainPanel = new JPanel();
JPanel sizePanel = new JPanel();
Border b1 = BorderFactory.createTitledBorder("Size");
sizePanel.setBorder(b1);
ButtonGroup sizeGroup = new ButtonGroup();
small = new JRadioButton("Small");
small.setSelected(true);
sizePanel.add(small);
sizeGroup.add(small);
medium = new JRadioButton("medium");
sizePanel.add(medium);
sizeGroup.add(medium);
large = new JRadioButton("large");
sizePanel.add(large);
sizeGroup.add(large);
mainPanel.add(sizePanel);
JPanel topPanel = new JPanel();
Border b2 = BorderFactory.createTitledBorder("Toppings");
topPanel.setBorder(b2);
pepperoni = new JCheckBox("pepperoni");
topPanel.add(pepperoni);
mushrooms = new JCheckBox("Mushrooms");
topPanel.add(mushrooms);
anchovies = new JCheckBox("anchovies");
topPanel.add(anchovies);
mainPanel.add(topPanel);
buttonOK = new JButton("OK");
buttonOK.addActionListener(b1);
mainPanel.add(buttonOK);
this.add(mainPanel);
this.setVisible(true);
}
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == buttonOK)
{
String tops = "";
if ( pepperoni.isSelected())
tops += "pepperoni \n";
if ( mushrooms.isSelected())
tops += "mushrooms \n";
if ( anchovies.isSelected())
tops += "anchovies \n";
String msg = " You ordered a " ;
if ( small.isSelected())
msg += "small pizza with " ;
if ( medium.isSelected())
msg += "medium pizza with " ;
if ( large.isSelected())
msg += "large pizza with " ;
if (tops.equals(" "))
msg += " no toppings";
else
msg += "the following toppings with : \n" + tops;
JOptionPane.showMessageDialog(buttonOK, msg, "Your Order", JOptionPane.INFORMATION_MESSAGE);
pepperoni.setSelected(false);
mushrooms.setSelected(false);
anchovies.setSelected(false);
small.setSelected(true);
}
}
}
}
Re: Help would be so much appreciated new Irish Java programmer
For future reference, please surround your code with the code tags
Error 1: You've redefined a variable name, which cannot be done
Code java:
ButtonListener b1 = new ButtonListener();
...
Border b1 = BorderFactory.createTitledBorder("Size");
Error 2: bl was first defined as a ButtonListener, so you cannot set the border of a component with a button listener.
The compile errors should identify the exact line the line where the error occurs, which should help identify the underlying problem.