Java Error: Exception in thread “main” java.lang.IllegalArgumentException: adding a window to a container

送分小仙女□ 提交于 2019-12-13 03:53:55

问题


I am currently creating a Calculator project in java and right now I am only making the design or the GUI for it. This is my code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

    public class Calculator extends JFrame {

          JButton jcomp1;
             JButton jcomp2;
          JButton jcomp3;
            JTextField jcomp4;
            JTextField jcomp5;
            JRadioButton rb1;
            JRadioButton jcomp7;
            JRadioButton jcomp8;
            JRadioButton jcomp9;
            JRadioButton jcomp10;
            JRadioButton jcomp11;
            JLabel jcomp12;
            JLabel jcomp13;
            JButton jcomp14;
            JButton jcomp15;
            JButton jcomp16;
            JButton jcomp17;
            JButton jcomp18;
            JButton jcomp19;
            JButton jcomp20;
            JButton jcomp21;
            JButton jcomp22;
            JButton jcomp23;
            JButton jcomp24;
            JButton jcomp25;
            JButton jcomp26;
            JButton jcomp27;
            JButton jcomp28;
            JButton jcomp29;
            JButton jcomp30;
            JButton jcomp31;
            JButton jcomp32;
            JButton jcomp33;
            JButton jcomp34;
            JButton jcomp35;

            public Calculator() {

                jcomp1 = new JButton ("A");
                jcomp2 = new JButton ("CE");
                jcomp3 = new JButton ("C");
                jcomp4 = new JTextField (5);
                jcomp5 = new JTextField (5);
                rb1 = new JRadioButton ("Hex");
                jcomp7 = new JRadioButton ("Dec");
                jcomp8 = new JRadioButton ("Oct");
                jcomp9 = new JRadioButton ("Bin");
                jcomp10 = new JRadioButton ("On");
                jcomp11 = new JRadioButton ("Off");
                jcomp12 = new JLabel ("Palindrome");
                jcomp13 = new JLabel ("Test");
                jcomp14 = new JButton ("?");
                jcomp15 = new JButton ("B");
                jcomp16 = new JButton ("C");
                jcomp17 = new JButton ("D");
                jcomp18 = new JButton ("E");
                jcomp19 = new JButton ("F");
                jcomp20 = new JButton ("7");
                jcomp21 = new JButton ("4");
                jcomp22 = new JButton ("1");
                jcomp23 = new JButton ("0");
                jcomp24 = new JButton ("8");
                jcomp25 = new JButton ("5");
                jcomp26 = new JButton ("2");
                jcomp27 = new JButton (".");
                jcomp28 = new JButton ("3");
                jcomp29 = new JButton ("6");
                jcomp30 = new JButton ("9");
                jcomp31 = new JButton ("+");
                jcomp32 = new JButton ("-");
                jcomp33 = new JButton ("/");
                jcomp34 = new JButton ("*");
                jcomp35 = new JButton ("=");

                setPreferredSize (new Dimension (363, 312));
                setLayout (null);

                add (jcomp1);
                add (jcomp2);
                add (jcomp3);
                add (jcomp4);
                add (jcomp5);
                add (rb1);
                add (jcomp7);
                add (jcomp8);
                add (jcomp9);
                add (jcomp10);
                add (jcomp11);
                add (jcomp12);
                add (jcomp13);
                add (jcomp14);
                add (jcomp15);
                add (jcomp16);
                add (jcomp17);
                add (jcomp18);
                add (jcomp19);
                add (jcomp20);
                add (jcomp21);
                add (jcomp22);
                add (jcomp23);
                add (jcomp24);
                add (jcomp25);
                add (jcomp26);
                add (jcomp27);
                add (jcomp28);
                add (jcomp29);
                add (jcomp30);
                add (jcomp31);
                add (jcomp32);
                add (jcomp33);
                add (jcomp34);
                add (jcomp35);

                jcomp1.setBounds (90, 100, 50, 30);
                jcomp2.setBounds (145, 100, 50, 30);
                jcomp3.setBounds (200, 100, 50, 30);
                jcomp4.setBounds (0, 0, 365, 40);
                jcomp5.setBounds (0, 45, 365, 40);
                rb1.setBounds (0, 100, 50, 25);
                jcomp7.setBounds (0, 120, 70, 25);
                jcomp8.setBounds (0, 145, 70, 25);
                jcomp9.setBounds (0, 170, 60, 25);
                jcomp10.setBounds (0, 245, 65, 25);
                jcomp11.setBounds (0, 270, 65, 25);
                jcomp12.setBounds (5, 205, 75, 25);
                jcomp13.setBounds (20, 220, 45, 25);
                jcomp14.setBounds (255, 100, 105, 30);
                jcomp15.setBounds (90, 135, 50, 30);
                jcomp16.setBounds (90, 170, 50, 30);
                jcomp17.setBounds (90, 205, 50, 30);
                jcomp18.setBounds (90, 240, 50, 30);
                jcomp19.setBounds (90, 275, 50, 30);
                jcomp20.setBounds (145, 135, 50, 30);
                jcomp21.setBounds (145, 170, 50, 30);
                jcomp22.setBounds (145, 205, 50, 30);
                jcomp23.setBounds (145, 240, 105, 30);
                jcomp24.setBounds (200, 135, 50, 30);
                jcomp25.setBounds (200, 170, 50, 30);
                jcomp26.setBounds (200, 205, 50, 30);
                jcomp27.setBounds (255, 240, 50, 30);
                jcomp28.setBounds (255, 205, 50, 30);
                jcomp29.setBounds (255, 170, 50, 30);
                jcomp30.setBounds (255, 135, 50, 30);
                jcomp31.setBounds (310, 135, 50, 30);
                jcomp32.setBounds (310, 170, 50, 30);
                jcomp33.setBounds (310, 205, 50, 30);
                jcomp34.setBounds (310, 240, 50, 30);
                jcomp35.setBounds (145, 275, 215, 30);

            }

            public static void main (String[] args) {
                Calculator calc = new Calculator();
                calc.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
                calc.getContentPane().add (new Calculator());
                calc.pack();
                calc.setVisible (true);
            }


    }

I am using eclipse to make this layout but I'm afraid I keep getting the error:

 Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container
        at java.awt.Container.checkNotAWindow(Container.java:429)
        at java.awt.Container.addImpl(Container.java:1037)
        at java.awt.Container.add(Container.java:363)
        at Calculator.main(Calculator.java:162)

Can you point out what did I do wrong? I tried making a design in GuiGenie but also I still get this error.


回答1:


The problem:

calc.getContentPane().add (new Calculator());

All of your buttons, text fields, etc., should be added to a JPanel, and the JPanel should be added to the content pane of the JFrame.

To do this:

  • create a JPanel in the constructor of your Calculator.
  • add all your JComponents to the JPanel.
  • in the Calculator constructor, add this code: this.getContentPane().add(jPanel);



回答2:


On this line:

calc.getContentPane().add (new Calculator());

You are attempting to add a calculator to itself. Quite obviously, this is not going to work. You really only need these two lines:

calc.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
calc.setVisible (true);

And they should be in the constructor for Calculator, not in your main method.



来源:https://stackoverflow.com/questions/14804603/java-error-exception-in-thread-main-java-lang-illegalargumentexception-addin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!