0

我正在开发一个基于 Java Swing GUI 的计算器。我在运行代码以测试我到目前为止所做的事情时遇到问题。当我编译它时,它返回以下异常。

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. 然后你可以将它添加到JFrame. 或者直接使Calculator_Project类的实例可见。

于 2013-02-15T07:16:44.647 回答
2

用 JPanal 而不是 JFrame 扩展你的类 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 回答