943,568 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 407
  • Java RSS
Nov 18th, 2008
0

Help with an error...

Expand Post »
I am getting the following error...

init:
deps-jar:
compile-single:
run-single:
Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container
at java.awt.Container.addImpl(Container.java:1010)
at java.awt.Container.add(Container.java:928)
at javax.swing.JFrame.addImpl(JFrame.java:480)
at java.awt.Container.add(Container.java:899)
at BoardCreator.<init>(BoardCreator.java:40)
at Board.main(Board.java:14)
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)

Here is my code which is a box that someone who would want to buy a skateboard would check off and the price would be displyed...

import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JRadioButton;


 
public class DeckPanel extends JFrame
{
  
   
   private final double MASTERTHRASHER = 60.00;
   private final double DICTATOR = 45.00;
   private final double STREETKING = 50.00;
   
   
   private JRadioButton masterThrasher;
   private JRadioButton dictator;
   private JRadioButton streetKing;
   
   public DeckPanel()
   {
       setLayout(new GridLayout(3,1));
       
       masterThrasher = new JRadioButton("The Master Thrasher: $60");
       dictator = new JRadioButton("The Dictator");
       streetKing = new JRadioButton("The Street King");
       
       add(masterThrasher);
       add(dictator);
       add(streetKing);
   }
   
   public double getDeckCost()
   {
       double deckCost = 0;
       
       if (masterThrasher.isSelected())
           deckCost = MASTERTHRASHER;
       if (dictator.isSelected())
           deckCost = DICTATOR;
       if (streetKing.isSelected())
           deckCost = STREETKING;
       return deckCost;
       
   }
}
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JRadioButton;


 
public class TruckPanel extends JFrame
{
  
   
   private final double TRUCK1 = 35.00;
   private final double TRUCK2 = 40.00;
   private final double TRUCK3 = 45.00;
   
   
   private JRadioButton truck1;
   private JRadioButton truck2;
   private JRadioButton truck3;
   
   public TruckPanel()
   {
       setLayout(new GridLayout(3,1));
       
       truck1 = new JRadioButton("7.75 in axle");
       truck2 = new JRadioButton("8 in axle");
       truck3 = new JRadioButton("8.5 in axle");
       
       add(truck1);
       add(truck2);
       add(truck3);
   }
   
   public double getTruckCost()
   {
       double truckCost = 0;
       
       if (truck1.isSelected())
           truckCost = TRUCK1;
       if (truck2.isSelected())
           truckCost = TRUCK2;
       if (truck3.isSelected())
           truckCost = TRUCK3;
       return truckCost;
       
   }
}
import java.awt.GridLayout;
import javax.swing.JCheckBox;
import javax.swing.JFrame;


  
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JRadioButton;


 
public class WheelPanel extends JFrame
{
  
   
   private final double WHEEL1 = 35.00;
   private final double WHEEL2 = 40.00;
   private final double WHEEL3 = 45.00;
   
   
   private JRadioButton wheel1;
   private JRadioButton wheel2;
   private JRadioButton wheel3;
   
   public WheelPanel()
   {
       setLayout(new GridLayout(3,1));
       
       wheel1 = new JRadioButton("7.75 in axle");
       wheel2 = new JRadioButton("8 in axle");
       wheel3 = new JRadioButton("8.5 in axle");
       
       add(wheel1);
       add(wheel2);
       add(wheel3);
   }
   
   public double getWheelCost()
   {
       double wheelCost = 0;
       
       if (wheel1.isSelected())
           wheelCost = WHEEL1;
       if (wheel2.isSelected())
           wheelCost = WHEEL2;
       if (wheel3.isSelected())
           wheelCost = WHEEL3;
       return wheelCost;
       
   }
}
public class ExtraPanel extends JFrame { private final double EXTRA1 = 20.00; private final double EXTRA2 = 22.00; private final double EXTRA3 = 24.00; private final double EXTRA4 = 28.00; private JCheckBox extra1; private JCheckBox extra2; private JCheckBox extra3; private JCheckBox extra4; public ExtraPanel() { setLayout(new GridLayout(3,1)); extra1 = new JCheckBox("51mm"); extra2 = new JCheckBox("55mm"); extra3 = new JCheckBox("58mm"); extra4 = new JCheckBox("61mm"); add(extra1); add(extra2); add(extra3); add(extra4); } public double getExtraCost() { double extraCost = 0; if (extra1.isSelected()) extraCost += EXTRA1; if (extra2.isSelected()) extraCost += EXTRA2; if (extra3.isSelected()) extraCost += EXTRA3; if (extra4.isSelected()) extraCost += EXTRA4; return extraCost; } }

import javax.swing.JLabel;
import javax.swing.JPanel;





public class Banner extends JPanel
{
    public Banner()
    {
        JLabel greeting = new JLabel("Hey Dude, build your custom board");
        
        add(greeting);
        
        
    }
    
    
    
    
    
    

}

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;




public class BoardCreator extends JFrame
{
    
    private double taxRate = .06;
    
    private DeckPanel deck;
    private TruckPanel truck;
    private WheelPanel wheel;
    private ExtraPanel extra;
    private Banner banner;
    
    private JPanel buttonPanel;
    private JButton calcButton;
    private JButton exitButton;
    
    
    public BoardCreator()
    {
        super ("Skate Shop");
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        setLayout(new BorderLayout());
        
        banner = new Banner();
        deck = new DeckPanel();
        truck = new TruckPanel();
        wheel = new WheelPanel();
        extra = new ExtraPanel();
        
        buildBoardCreator();
        
        add(banner, BorderLayout.NORTH);
        add(deck, BorderLayout.WEST);
        add(truck, BorderLayout.CENTER);
        add(wheel, BorderLayout.EAST);
        add(extra, BorderLayout.SOUTH);
        
        pack();
        setVisible(true);
    }
        
        private void buildBoardCreator()
        {
            buttonPanel = new JPanel();
            calcButton = new JButton("Calculate");
            exitButton = new JButton("Exit");
            
            calcButton.addActionListener(new CalcButtonListener());
            exitButton.addActionListener(new ExitButtonListener());
            
            buttonPanel.add(calcButton);
            buttonPanel.add(exitButton);
            
        }
        
        private class CalcButtonListener implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
                double subtotal;
                double tax;
                double total;
                
                subtotal = deck.getDeckCost()+
                        truck.getTruckCost()+
                        wheel.getWheelCost()+
                        extra.getExtraCost();
                tax = subtotal + taxRate;
                
                total = subtotal + tax;
                
                DecimalFormat dollar = new DecimalFormat("0.00");
                
                JOptionPane.showMessageDialog(null, "Subtotal: $"+
                        dollar.format(subtotal) + "\n" +
                        "Tax: $" + dollar.format(tax) + "\n" +
                        "Total: $" + dollar.format(total));
            }
        }
        
        private class ExitButtonListener implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
                System.exit(0);
            }
        }
}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Cory
 */
public class Board 
{
    public static void main(String[] args)
    {
        new BoardCreator();
    }

}
Similar Threads
Reputation Points: 4
Solved Threads: 0
Light Poster
cproud21 is offline Offline
42 posts
since Sep 2008
Nov 18th, 2008
0

Re: Help with an error...

Well, take a look at line 40, since that is where the error is occurring.
Java Syntax (Toggle Plain Text)
  1. at BoardCreator.<init>(BoardCreator.java:40)
.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007
Nov 18th, 2008
0

Re: Help with an error...

Im not seeing any error in that line, it is set up the same way as the line above it, which doesn't show an error, and copied in the same manner as in my book..
Reputation Points: 4
Solved Threads: 0
Light Poster
cproud21 is offline Offline
42 posts
since Sep 2008
Nov 18th, 2008
0

Re: Help with an error...

Well, perhaps you should post that line then, because personally I don't want to spend time counting down 40 lines in all that code that you posted (and it's doubtful that anyone else does either).
And read carefully the description of the error - it's pretty straightforward.
Last edited by Ezzaral; Nov 18th, 2008 at 4:30 pm.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007
Nov 18th, 2008
0

Re: Help with an error...

Java Syntax (Toggle Plain Text)
  1.  
  2. add(banner, BorderLayout.NORTH);
  3. add(deck, BorderLayout.WEST);
  4. add(truck, BorderLayout.CENTER);
  5. add(wheel, BorderLayout.EAST);
  6. add(extra, BorderLayout.SOUTH);
  7. ///
  8. adding a window to a container!
redefine Your classes as ...
Java Syntax (Toggle Plain Text)
  1.  
  2. public class ...Panel extends JPanel // NOT JFrame
quuba
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: tab separated file
Next Thread in Java Forum Timeline: Listing questions





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC