Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9305243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T23:53:05+00:00 2026-06-18T23:53:05+00:00

I’m working on a Java Swing GUI-based calculator. I’m having a problem running the

  • 0

I’m working on a Java Swing GUI-based calculator. I’m having a problem running the code to test what I did so far. When I compile it, it returns with following exception.

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)

here’s the code:

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);

}
 }  

How to resolve it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T23:53:06+00:00Added an answer on June 18, 2026 at 11:53 pm

    You are trying to add JFrame in to a JFrame and that’s why you are seeing this error.

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

    Either your Calculator_Project class should extend JPanel. And then you can add it into JFrame. Or directly make Calculator_Project class’ instance visible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have been unable to fix a problem with Java Unicode and encoding. The
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.