0

私はJavaSwingGUIベースの計算機に取り組んでいます。これまでに行ったことをテストするためのコードの実行に問題があります。コンパイルすると、次の例外で返されます。

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 javax.swing.JLayeredPane.addImpl(JLayeredPane.java:212)
    at java.awt.Container.add(Container.java:925)
    at javax.swing.JRootPane.setContentPane(JRootPane.java:608)
    at javax.swing.JFrame.setContentPane(JFrame.java:671)
    at calculator_project.Calculator_Project.main(Calculator_Project.java:184)
Java Result: 1
BUILD SUCCESSFUL (total time: 4 seconds)

コードは次のとおりです。

package calculator_project;

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

public class Calculator_Project extends JFrame {


private JButton add, subtract, multiply, divide, left_paren, right_paren,
        zero, one, two, three, four, five, six,seven, eight, nine;

private JTextField textfield;


public Calculator_Project(){


JPanel panel = new JPanel();

GridBagLayout layout = new GridBagLayout(); 

GridBagConstraints constraint = new GridBagConstraints(); 

panel.setLayout(layout);

textfield = new JTextField(10); 
constraint.fill= GridBagConstraints.HORIZONTAL;
constraint.gridx = 0;
constraint.gridy = 0;
panel.add(textfield);


add = new JButton("+"); 
constraint.fill= GridBagConstraints.HORIZONTAL;
constraint.gridx = 3;
constraint.gridy = 1;
panel.add(add,constraint);

subtract = new JButton("-"); 
constraint.fill= GridBagConstraints.HORIZONTAL;
constraint.gridx = 3;
constraint.gridy = 2;
panel.add(subtract,constraint);

multiply = new JButton("*"); 
constraint.fill= GridBagConstraints.HORIZONTAL;
constraint.gridx = 3;
constraint.gridy = 3;
panel.add(multiply,constraint);

divide = new JButton("/"); 
constraint.fill= GridBagConstraints.HORIZONTAL;
constraint.gridx = 3;
constraint.gridy = 4;
panel.add(divide,constraint);

left_paren = new JButton("("); 
constraint.fill= GridBagConstraints.HORIZONTAL;
constraint.gridx = 0;
constraint.gridy = 4;
panel.add(left_paren,constraint);


right_paren = new JButton(")"); 
constraint.fill= GridBagConstraints.HORIZONTAL;
constraint.gridx = 2;
constraint.gridy = 4;
panel.add(right_paren,constraint);

zero = new JButton("0"); 
constraint.fill= GridBagConstraints.HORIZONTAL;
constraint.gridx = 1;
constraint.gridy = 4;
panel.add(zero,constraint);

one = new JButton("1"); 
constraint.fill= GridBagConstraints.HORIZONTAL;
constraint.gridx = 0;
constraint.gridy = 3;
panel.add(one,constraint);

two = new JButton("2"); 
constraint.fill= GridBagConstraints.HORIZONTAL;
constraint.gridx = 1;
constraint.gridy = 3;
panel.add(two,constraint);

three = new JButton("3"); 
constraint.fill= GridBagConstraints.HORIZONTAL;
constraint.gridx = 2;
constraint.gridy = 3;
panel.add(three,constraint);

four = new JButton("4"); 
constraint.fill= GridBagConstraints.HORIZONTAL;
constraint.gridx = 0;
constraint.gridy = 2;
panel.add(four,constraint);

five = new JButton("5"); 
constraint.fill= GridBagConstraints.HORIZONTAL;
constraint.gridx = 1;
constraint.gridy = 2;
panel.add(five,constraint);

six = new JButton("6"); 
constraint.fill= GridBagConstraints.HORIZONTAL;
constraint.gridx = 2;
constraint.gridy = 2;
panel.add(six,constraint);

seven = new JButton("7"); 
constraint.fill= GridBagConstraints.HORIZONTAL;
constraint.gridx = 0;
constraint.gridy = 1;
panel.add(seven,constraint);

eight = new JButton("8"); 
constraint.fill= GridBagConstraints.HORIZONTAL;
constraint.gridx = 1;
constraint.gridy = 1;
panel.add(eight,constraint);

nine = new JButton("9"); 
constraint.fill= GridBagConstraints.HORIZONTAL;
constraint.gridx = 2;
constraint.gridy = 1;
panel.add(nine,constraint);


event e = new event();
add.addActionListener(e);
subtract.addActionListener(e);
multiply.addActionListener(e);
divide.addActionListener(e);
left_paren.addActionListener(e);
right_paren.addActionListener(e);
zero.addActionListener(e);
one.addActionListener(e);
two.addActionListener(e);
three.addActionListener(e);
four.addActionListener(e);
five.addActionListener(e);
six.addActionListener(e);
seven.addActionListener(e);
eight.addActionListener(e);
nine.addActionListener(e);


}

 public class event implements ActionListener {

public void actionPerformed  (ActionEvent e){

    double number;

    try{

        number= Double.parseDouble(textfield.getText());

    } catch (NumberFormatException ee){

        textfield.setText("illegal entry!");
        textfield.setForeground(Color.RED);

        return;

    }


}
}

public static void main(String[] args) {
    JFrame frame = new JFrame("Calculator");
    Calculator_Project panel = new Calculator_Project();
    frame.setContentPane(panel);
    frame.setSize(300,150);
    frame.setVisible(true);

}
 }  

それを解決する方法は?

4

2 に答える 2

5

JFrame を JFrame に追加しようとしているため、このエラーが表示されます。

Calculator_Project panel = new Calculator_Project();
frame.setContentPane(panel);

あなたのCalculator_Projectクラスは拡張する必要がありJPanelます。そして、それを に追加できますJFrameCalculator_Projectまたは、クラスのインスタンスを直接表示します。

于 2013-02-15T07:16:44.647 に答える
2

JFrame.hereの代わりにJPanalを使用してクラスCalculator_Projectを拡張します。コードは機能しています...

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

public class Calculator_Project extends JPanel {


private JButton add, subtract, multiply, divide, left_paren, right_paren,
    zero, one, two, three, four, five, six,seven, eight, nine;

private JTextField textfield;


public Calculator_Project(){


JPanel panel = new JPanel();

GridBagLayout layout = new GridBagLayout();

GridBagConstraints constraint = new GridBagConstraints();

panel.setLayout(layout);

textfield = new JTextField(10);
constraint.fill= GridBagConstraints.HORIZONTAL;
constraint.gridx = 0;
constraint.gridy = 0;
panel.add(textfield);


add = new JButton("+");
constraint.fill= GridBagConstraints.HORIZONTAL;
constraint.gridx = 3;
constraint.gridy = 1;
panel.add(add,constraint);

subtract = new JButton("-");
constraint.fill= GridBagConstraints.HORIZONTAL;
constraint.gridx = 3;
constraint.gridy = 2;
panel.add(subtract,constraint);

multiply = new JButton("*");
constraint.fill= GridBagConstraints.HORIZONTAL;
constraint.gridx = 3;
constraint.gridy = 3;
panel.add(multiply,constraint);

divide = new JButton("/");
constraint.fill= GridBagConstraints.HORIZONTAL;
constraint.gridx = 3;
constraint.gridy = 4;
panel.add(divide,constraint);

left_paren = new JButton("(");
constraint.fill= GridBagConstraints.HORIZONTAL;
constraint.gridx = 0;
constraint.gridy = 4;
panel.add(left_paren,constraint);


right_paren = new JButton(")");
constraint.fill= GridBagConstraints.HORIZONTAL;
constraint.gridx = 2;
constraint.gridy = 4;
panel.add(right_paren,constraint);

zero = new JButton("0");
constraint.fill= GridBagConstraints.HORIZONTAL;
 constraint.gridx = 1;
constraint.gridy = 4;
panel.add(zero,constraint);

one = new JButton("1");
constraint.fill= GridBagConstraints.HORIZONTAL;
constraint.gridx = 0;
constraint.gridy = 3;
panel.add(one,constraint);

two = new JButton("2");
constraint.fill= GridBagConstraints.HORIZONTAL;
constraint.gridx = 1;
constraint.gridy = 3;
panel.add(two,constraint);

three = new JButton("3");
constraint.fill= GridBagConstraints.HORIZONTAL;
constraint.gridx = 2;
constraint.gridy = 3;
panel.add(three,constraint);

 four = new JButton("4");
 constraint.fill= GridBagConstraints.HORIZONTAL;
constraint.gridx = 0;
constraint.gridy = 2;
panel.add(four,constraint);

five = new JButton("5");
constraint.fill= GridBagConstraints.HORIZONTAL;
constraint.gridx = 1;
constraint.gridy = 2;
panel.add(five,constraint);

 six = new JButton("6");
constraint.fill= GridBagConstraints.HORIZONTAL;
constraint.gridx = 2;
constraint.gridy = 2;
panel.add(six,constraint);

seven = new JButton("7");
constraint.fill= GridBagConstraints.HORIZONTAL;
constraint.gridx = 0;
constraint.gridy = 1;
panel.add(seven,constraint);

eight = new JButton("8");
constraint.fill= GridBagConstraints.HORIZONTAL;
constraint.gridx = 1;
constraint.gridy = 1;
panel.add(eight,constraint);

nine = new JButton("9");
constraint.fill= GridBagConstraints.HORIZONTAL;
constraint.gridx = 2;
constraint.gridy = 1;
panel.add(nine,constraint);


event e = new event();
add.addActionListener(e);
subtract.addActionListener(e);
multiply.addActionListener(e);
divide.addActionListener(e);
left_paren.addActionListener(e);
right_paren.addActionListener(e);
zero.addActionListener(e);
one.addActionListener(e);
two.addActionListener(e);
three.addActionListener(e);
four.addActionListener(e);
five.addActionListener(e);
six.addActionListener(e);
seven.addActionListener(e);
eight.addActionListener(e);
nine.addActionListener(e);

 add(panel);
 }

 public class event implements ActionListener {

 public void actionPerformed  (ActionEvent e){

  double number;

  try{

    number= Double.parseDouble(textfield.getText());

  } catch (NumberFormatException ee){

    textfield.setText("illegal entry!");
    textfield.setForeground(Color.RED);

    return;

  }


  }
 }

 public static void main(String[] args) {
 JFrame frame = new JFrame("Calculator");
 Calculator_Project panel = new Calculator_Project();
 frame.setContentPane(panel);
 frame.setSize(300,150);
 frame.setVisible(true);

 }
 }  
于 2013-02-15T07:34:19.000 に答える