Results 1 to 11 of 11
  1. #1
    liz0983 is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Post Problem with App, used a template, not working correctly.

    Hello,
    Im having a problem with my second Applet for my java programming class. The professor gave out a template sheet to use as a reference and I filled in everything as needed to produce the program. It just isnt working correctly. Thank you for any help or advice.


    It should look like this:
    Problem with App, used a template, not working correctly.-applet2.png


    and heres the code:
    ____________________________________
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.border.*;
    import java.text.*;
    
    public class Assignment8 extends JFrame implements ActionListener, ItemListener
    {
    	double sPrice = 6.99, mPrice = 8.99, lPrice = 10.99;
    	double totalPrice = sPrice;
    	
    	ButtonGroup sizeGrp = new ButtonGroup();
    	JRadioButton sRB = new JRadioButton("Small",true);
    	JRadioButton mRB = new JRadioButton("Medium",false);
    	JRadioButton lRB = new JRadioButton("Large",false);
    
    	double sausagePrice = 1.49, pepperoniPrice = 1.49, salamiPrice = 1.49, 
    			olivesPrice = 0.99, mushroomsPrice = 0.99, anchoviesPrice = 0.99;
    
    	JCheckBox sausageBox = new JCheckBox("Sausage",false);
    	JCheckBox pepperoniBox = new JCheckBox("Pepperoni",false);
    	JCheckBox salamiBox = new JCheckBox("Salami",false);
    	JCheckBox olivesBox = new JCheckBox("Olives",false);
    	JCheckBox mushroomsBox = new JCheckBox("Mushrooms",false);
    	JCheckBox anchoviesBox = new JCheckBox("Anchovies",false);
    	
    	JButton exit = new JButton("Exit");
    	
    	JPanel topPanel = new JPanel(new FlowLayout());
    	JPanel middlePanel = new JPanel(new FlowLayout());
    	JPanel bottomPanel = new JPanel(new FlowLayout());
    	
    	JTextField totPrice = new JTextField(10);
    	
    	Container con = getContentPane();
    	
    	NumberFormat currency = NumberFormat.getCurrencyInstance();
    	
    public Assignment8()
    {
    	super("Pizza Calculator");
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	con.setLayout(new BorderLayout());
    
    	sizeGrp.add(sRB);
    	sRB.addItemListener(this);
    	sizeGrp.add(mRB);
    	mRB.addItemListener(this);
    	sizeGrp.add(lRB);
    	lRB.addItemListener(this);
    	
    	Border topBorder = BorderFactory.createEtchedBorder();
    	topBorder = BorderFactory.createTitledBorder(topBorder,"Size");
    	topPanel.add(sRB);
    	topPanel.add(mRB);
    	topPanel.add(lRB);
    
    	Border middleBorder = BorderFactory.createEtchedBorder();
    	middleBorder = BorderFactory.createTitledBorder(middleBorder,"Toppings");
    	middlePanel.add(sausageBox);
    	middlePanel.add(pepperoniBox);
    	middlePanel.add(salamiBox);
    	middlePanel.add(olivesBox);
    	middlePanel.add(mushroomsBox);
    	middlePanel.add(anchoviesBox);
    
    	Border bottomBorder = BorderFactory.createEtchedBorder();
    	bottomBorder = BorderFactory.createTitledBorder(bottomBorder);
    
    	totPrice.setText(currency.format(totalPrice));
    	bottomPanel.add(totPrice);
    
    	bottomPanel.add(exit);
    	exit.addActionListener(this);
    
    	con.add(bottomPanel, BorderLayout.SOUTH);
    }
    
    public static void main(String[] arguments)
    {
    	Assignment8 aFrame = new Assignment8();
    	aFrame.setSize(450,225);
    	aFrame.setResizable(false);
    	aFrame.setVisible(true);
    }
    
    public void itemStateChanged(ItemEvent check)
    {
    	Object source = check.getItem();
    	if(source == sRB)
    	{
    		int select = check.getStateChange();
    		if(select == ItemEvent.SELECTED)
    			totalPrice += sPrice;
    		else if(select == ItemEvent.DESELECTED)
    			totalPrice -= sPrice;
    	}
    	
    	if(source == mRB)
    	{
    		int select = check.getStateChange();
    		if(select == ItemEvent.SELECTED)
    			totalPrice += mPrice;
    		else if(select == ItemEvent.DESELECTED)
    			totalPrice -= mPrice;
    	}
    
    	if(source == sausageBox)
    	{
    		int select = check.getStateChange();
    		if(select == ItemEvent.SELECTED)
    			totalPrice += sausagePrice;
    		else if(select == ItemEvent.DESELECTED)
    			totalPrice -= sausagePrice;
    	}
    
    	if(source == pepperoniBox)
    	{
    		int select = check.getStateChange();
    		if(select == ItemEvent.SELECTED)
    			totalPrice += pepperoniPrice;
    		else if(select == ItemEvent.DESELECTED)
    			totalPrice -= pepperoniPrice;
    	}
    
    	if(source == salamiBox)
    	{
    		int select = check.getStateChange();
    		if(select == ItemEvent.SELECTED)
    			totalPrice += salamiPrice;
    		else if(select == ItemEvent.DESELECTED)
    			totalPrice -= salamiPrice;
    	}
    
    	if(source == olivesBox)
    	{
    		int select = check.getStateChange();
    		if(select == ItemEvent.SELECTED)
    			totalPrice += olivesPrice;
    		else if(select == ItemEvent.DESELECTED)
    			totalPrice -= olivesPrice;
    	}
    
    	if(source == mushroomsBox)
    	{
    		int select = check.getStateChange();
    		if(select == ItemEvent.SELECTED)
    			totalPrice += mushroomsPrice;
    		else if(select == ItemEvent.DESELECTED)
    			totalPrice -= mushroomsPrice;
    	}
    
    	if(source == anchoviesBox)
    	{
    		int select = check.getStateChange();
    		if(select == ItemEvent.SELECTED)
    			totalPrice += anchoviesPrice;
    		else if(select == ItemEvent.DESELECTED)
    			totalPrice -= anchoviesPrice;
    	}
    
    	totPrice.setText(currency.format(totalPrice));
    		
    }
    
     public void actionPerformed(ActionEvent e)
     {
    	Object source = e.getSource();
    	if(source == exit)
    	System.exit(0);
    
     }
    }
    Last edited by Norm; 04-13-2012 at 01:50 AM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Problem with Applet, used a template, not working correctly.

    It just isnt working correctly
    Please explain what is not working correctly.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    liz0983 is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: Problem with Applet, used a template, not working correctly.

    The only part working is the text field and the exit button.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Problem with Applet, used a template, not working correctly.

    That's good that some of it works. Now pick one part that does not work and explain all about what happens or does not happen.

    Post moved from Applet section. Nothing to do with java applets.
    Last edited by Norm; 04-13-2012 at 03:11 AM. Reason: Moved thread
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    liz0983 is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: Problem with App, used a template, not working correctly.

    The top two panels are blank, I can't see my radio buttons or check boxes.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Problem with App, used a template, not working correctly.

    Where do you add those panels to the GUI so they can be shown?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    liz0983 is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: Problem with App, used a template, not working correctly.

    Between lines 80-97.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Problem with App, used a template, not working correctly.

    What is the line number where you add the top panel to the GUI.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    liz0983 is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: Problem with App, used a template, not working correctly.

    Number line 80

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Problem with App, used a template, not working correctly.

    Are you using the line numbers from your post? Here are lines 79,80 and 81
    Java Code:
    public static void main(String[] arguments)
    {
        Assignment8 aFrame = new Assignment8();
    Line 80 is the {

    Look at the code you posted and use the line number from there.

    Hnt: You have NOT added the top panel to the GUI
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    liz0983 is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default Re: Problem with App, used a template, not working correctly.

    I was stating the number 80 from a smart phone and I see that it was not the exact line 80 now that I am logged into a regular computer.
    But I'll look into the "hint" you suggested.

Similar Threads

  1. method not working correctly
    By r1b in forum New To Java
    Replies: 4
    Last Post: 01-04-2012, 09:09 PM
  2. Replies: 2
    Last Post: 12-28-2011, 10:15 AM
  3. Applet not working correctly?
    By Beavotropper2 in forum Java Applets
    Replies: 2
    Last Post: 04-18-2011, 07:32 AM
  4. [SOLVED] \t not working correctly?
    By Gakusei in forum New To Java
    Replies: 5
    Last Post: 05-06-2008, 05:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •