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 のコンテンツ ペインに追加する必要があります。

これをする:

  • Calculator のコンストラクターで JPanel を作成します。
  • すべての JComponents を JPanel に追加します。
  • Calculator コンストラクターで、次のコードを追加します。this.getContentPane().add(jPanel);
于 2013-02-11T01:17:49.277 に答える
2

この行で:

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

電卓をそれ自体に追加しようとしています。明らかに、これは機能しません。本当に必要なのは次の 2 行だけです。

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

そして、それらはCalculatorメインメソッドではなく、のコンストラクターにある必要があります。

于 2013-02-11T01:17:37.860 に答える