0

我目前正在用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 中进行设计,但仍然出现此错误。

4

2 回答 2

3

问题:

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

您的所有按钮、文本字段等都应该添加到 JPanel 中,并且 JPanel 应该添加到 JFrame 的内容窗格中。

去做这个:

  • 在计算器的构造函数中创建一个 JPanel。
  • 将所有 JComponent 添加到 JPanel。
  • 在 Calculator 构造函数中,添加以下代码:this.getContentPane().add(jPanel);
于 2013-02-11T01:17:49.277 回答
2

在这条线上:

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

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

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

它们应该在 for 的构造函数中Calculator,而不是在您的 main 方法中。

于 2013-02-11T01:17:37.860 回答