Java错误:异常在线程“主” java.lang.IllegalArgumentException

2019-11-01 13:33发布

我目前使用Java创建一个计算器项目,现在我只是做设计或者为它的GUI。 这是我的代码:

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);
            }


    }

我使用Eclipse来使这个布局,但我怕我不断收到错误:

 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)

你能指出我做了什么错? 我试图使在GuiGenie设计,而且我仍然得到这个错误。

Answer 1:

问题:

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

你所有的按钮,文本字段等,应该被加入到一个JPanel,而JPanel中应加入到JFrame的内容窗格。

去做这个:

  • 在您的计算机的构造函数创建一个JPanel。
  • 添加所有JComponents到的JPanel。
  • 在计算器构造,添加以下代码: this.getContentPane().add(jPanel);


Answer 2:

在这条线:

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

您正在尝试一个计算器添加到自身。 很明显,这是行不通的。 你真的只需要两行:

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

他们应该在构造函数Calculator ,而不是在你的主要方法。



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