Question

I've just been making a GUI calculator in Java and I've been using eclipse, thing is I am not getting any syntax errors but when I compile I get a couple of compilation errors, and I couldn't figure out why. These are the errors I receive.

Exception in thread "main" java.lang.IllegalArgumentException: cannot add to layout: constraints must be a GridBagConstraint
at java.awt.GridBagLayout.addLayoutComponent(Unknown Source)

at java.awt.Container.addImpl(Unknown Source)

at java.awt.Container.add(Unknown Source)

at javax.swing.JFrame.addImpl(Unknown Source)

at java.awt.Container.add(Unknown Source)

at Calculator1.<init>(Calculator1.java:81)

at Calculator1.main(Calculator1.java:178)

This is my source code:

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

public class Calculator1 extends JFrame{


    JButton add, subtract, multiply, division;  //These are going to be all the buttons
    JTextField num1, num2; //All the fields I need
    JLabel result, enter1, enter2; //The labels

    public Calculator1(){ //Starting of constructor class

        setLayout(new GridBagLayout()); //Declaring I'm going to be using the GridBagLayout
        GridBagConstraints c = new GridBagConstraints(); //Making a GridBagLayout Object

        enter1 = new JLabel("1st :"); //New JLabel with 1st being put into enter1
        c.fill = GridBagConstraints.HORIZONTAL; //Will set the size 
        c.gridx = 0; //Position x
        c.gridy = 0; //Position y
        add(enter1, c); //Add options to our object c

        num1 = new JTextField(10); //How big can user enter a value upto
        c.fill = GridBagConstraints.HORIZONTAL; 
        c.gridx = 1;
        c.gridy = 0;
        c.gridwidth = 3;
        add(num1, c);

        enter2 = new JLabel("2nd :");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 1;
        c.gridheight = 1;
        add(enter2, c);

        num2 = new JTextField(10);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 1;
        c.gridwidth = 3;
        add(num2, c);

        add = new JButton("+");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 2;
        c.gridwidth = 1;
        add(add, c);

        subtract = new JButton("-");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 2;
        c.gridwidth = 1;
        add(subtract, c);

        multiply = new JButton("*");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 2;
        c.gridy = 2;
        c.gridwidth = 1;
        add(multiply, c);

        division = new JButton("/");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 3;
        c.gridy = 2;
        c.gridwidth = 1;
        add(division, c);

        result = new JLabel("");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 4;
        c.gridwidth = 4;
        add(result, add);

        event a = new event();
        add.addActionListener(a); //If user clicks add on the button, it should load the adding function from event class
        subtract.addActionListener(a);
        multiply.addActionListener(a);
        division.addActionListener(a);

    }

    public class event implements ActionListener{


        public void actionPerformed(ActionEvent a) { //This will be used to do what the user wants

            double number1, number2; 

            try{

                number1 = Double.parseDouble(num1.getText()); //This will convert num1 into a double

            } catch(NumberFormatException e){ //If user enters a value that's not a double such as a String, it will display this message

                result.setText("Illegal Data for first field"); 
                result.setForeground(Color.red);
                return;

            }

            try{

                number2 = Double.parseDouble(num2.getText());

            } catch(NumberFormatException e){

                result.setText("Illegal Data for second field");
                result.setForeground(Color.red);
                return;

            }

            String op = a.getActionCommand(); //Put whatever the user clicks into op, op is just a name

            if(op.equals("+")){

                double sum = number1 + number2;

                result.setText(number1 + "+" + number2 + "=" + sum);
                result.setForeground(Color.red);


            }
            else if(op.equals("-")){

                double diff = number1-number2;

                result.setText(number1 + "-" + number2 + "=" + diff);
                result.setForeground(Color.red);


            }
            else if(op.equals("*")){

                double times = number1*number2;

                result.setText(number1 + "*" + number2 + "=" + times);
                result.setForeground(Color.red);


            }
            else if(op.equals("/")){

            if(number2 == 0){

                result.setText("Cannot divide by 0 you dumb shit");
                result.setForeground(Color.red);
            }   

            else{

                double divide = number1/number2;

                result.setText(number1 + "/" + number2 + "=" + divide);
                result.setForeground(Color.red);

            }

            }

        }



    }

    public static void main(String args[]){

        Calculator1 Gui = new Calculator1(); //Creating our calculator Object
        Gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Gui.setVisible(true);
        Gui.setSize(250, 175);
        Gui.setTitle("My Java calculator");
    }


}
Was it helpful?

Solution

The error is a runtime rather then a compilation error - The variable add is a Swing component rather than the expected layout manager constraint. Use the following when adding the result component

add(result, c);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top