基于GUI的java计算器:向容器异常添加窗口

时间:2013-02-15 07:12:13

标签: java swing jframe

我正在研究基于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);

}
 }  

如何解决?

2 个答案:

答案 0 :(得分:5)

您正在尝试将JFrame添加到JFrame中,这就是您看到此错误的原因。

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

您的Calculator_Project课程应该延长JPanel。然后您可以将其添加到JFrame。或直接使Calculator_Project class'实例可见。

答案 1 :(得分: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);

 }
 }