Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help would be so much appreciated new Irish Java programmer

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question 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

    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);
    	}
     
    	}
    	}
    }
    Last edited by copeg; September 29th, 2010 at 02:20 PM. Reason: Please use the code tags


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,307
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default 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

        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.

  3. The Following User Says Thank You to copeg For This Useful Post:

    joan (September 29th, 2010)

Similar Threads

  1. Replies: 2
    Last Post: November 7th, 2010, 01:33 PM
  2. Replies: 2
    Last Post: August 1st, 2010, 06:29 AM
  3. Programmer for a Java based game project
    By Takkun in forum Project Collaboration
    Replies: 4
    Last Post: June 14th, 2010, 05:47 PM
  4. Hello everybody from armenian java programmer
    By planmaster in forum Member Introductions
    Replies: 0
    Last Post: April 12th, 2010, 03:01 PM
  5. Beginning java programmer!!!!!!!!!!!!!!!!!! need help
    By raidcomputer in forum Java Theory & Questions
    Replies: 3
    Last Post: September 15th, 2009, 08:52 PM