-4

I am trying to create a calculator that can perform simple arithmetic calculations but when i run the code given below only the latest button is being rendered onto the screen. If the code seems too simple that has to do with the fact that i am still very green regarding swings.

I have tried various other methods including a Jform but that ended up posing more questions than answers.

package javaapplication70;
import java.util.Scanner;
import java.awt.*;
import javax.swing.*;

public class calculatorGUI {
    public static void main(String args[]){
        Scanner in = new Scanner(System.in);
        JFrame j = new JFrame("Calculator");
        j.setSize(1000,1500);
        j.setDefaultCloseOperation(3);
        JTextArea jt = new JTextArea(10,40);
        j.add(jt);
        JButton jb15 = new JButton("C");
        j.add(jb15);
        JButton jb = new JButton("1");
        jb.setSize(7, 7);
        j.add(jb);
        JButton jb1 = new JButton("2");
        j.add(jb1);
        JButton jb2 = new JButton("3");
        j.add(jb2);
        JButton jb3 = new JButton("4");
        j.add(jb3);
        JButton jb4 = new JButton("5");
        j.add(jb4);
        JButton jb5 = new JButton("6");
        j.add(jb5);
        JButton jb6 = new JButton("7");
        j.add(jb6);
        JButton jb7 = new JButton("8");
        j.add(jb7);
        JButton jb8 = new JButton("9");
        j.add(jb8);
        JButton jb9 = new JButton("0");
        j.add(jb9);
        JButton jb10 = new JButton("+");
        j.add(jb10);
        JButton jb11 = new JButton("-");
        j.add(jb11);
        JButton jb12 = new JButton("x");
        j.add(jb12);
        JButton jb13 = new JButton("/");
        j.add(jb13);
        JButton jb14 = new JButton("=");
        j.add(jb14);
        int num1=0,num2,res;
        j.setVisible(true);
    }
}
  • 1
    Welcome Tarun, but it looks like you forgot to add your code. Please add it, so we can better help you. – gmauch Jul 10 '19 at 13:24
  • A JFrame uses a BorderLayout by default. You are attempting to add all the components to the CENTER of the BorderLayout which won't work because only a single component can be added to the CENTER. That component could be a JPanel with all your button. Check out: https://stackoverflow.com/questions/33739623/how-to-add-a-shortcut-key-for-a-jbutton-in-java/33739732#33739732 for a basic example to get you started. – camickr Jul 10 '19 at 14:44
  • See also this [calculator example](http://stackoverflow.com/a/7441804/418556). It uses `ScriptEngine` to evaluate the expression in the text field. – Andrew Thompson Jul 10 '19 at 17:52

1 Answers1

0

All of the buttons are there, they are simply stacked on top of each other in the same location, you should look into using layout managers to achieve the desired result. Check out the Oracle guide here

HamishD
  • 349
  • 2
  • 15
  • 1
    *they are simply stacked on top of each other in the same location* - Only a single component will be managed by the BorderLayout at the CENTER position. So only the last component added is managed by the BorderLayout. When the frame is made visible, the last button is given a size/location. For all the other components they have a size of (0, 0), so there is nothing to paint. So you should add a JPanel, using another layout manager and add that panel to the frame. Then add the buttons to the panel. – camickr Jul 10 '19 at 14:59