0

I am trying to make a simple calculator as a first project. I have written what I thought would work for a GUI but I am missing something important and it shows. The text field and buttons only show up when resizing the window. There is clearly a crucial piece to this missing! I have looked for an answer before asking, I apologize if I am posting an answered question. Here is my code:

import javax.swing.*;

public class Frame {
public Frame() {

JFrame Window = new JFrame("Calculator");
Window.setSize(500, 500);
Window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Window.setVisible(true);

JTextField textField3 = new JTextField();  
textField3.setBounds(50,20,230,50);
    textField3.setVisible(true);
 Window.add(textField3);


JButton One = new JButton("1");
One.setBounds(50,80,50,50);
Window.add(One);

JButton Two = new JButton("2");
Two.setBounds(110,80,50,50);
Window.add(Two);

JButton Three = new JButton("3");
Three.setBounds(170,80,50,50);
Window.add(Three);

JButton Four = new JButton("4");
Four.setBounds(50,140,50,50);
Window.add(Four);

JButton Five = new JButton("5");
Five.setBounds(110,140,50,50);
Window.add(Five);

JButton Six = new JButton("6");
Six.setBounds(170,140,50,50);
Window.add(Six);

JButton Seven = new JButton("7");
Seven.setBounds(50,200,50,50);
Window.add(Seven);

JButton Eight = new JButton("8");
Eight.setBounds(110,200,50,50);
Window.add(Eight);

JButton Nine = new JButton("9");
Nine.setBounds(170,200,50,50);
Window.add(Nine);

JButton Zero = new JButton("0");
Zero.setBounds(50,260,50,50);
Window.add(Zero);

JButton Decimal = new JButton(".");
Decimal.setBounds(110,260,50,50);
Window.add(Decimal);

JButton Equal = new JButton("=");
Equal.setBounds(170,260,50,50);
Window.add(Equal);

JButton Divide = new JButton("/");
Divide.setBounds(230,80,50,50);
Window.add(Divide);

JButton Mutiply = new JButton("*");
Mutiply.setBounds(230,140,50,50);
Window.add(Mutiply);

JButton Add = new JButton("+");
Add.setBounds(230,200,50,50);
Window.add(Add);

JButton Subtract = new JButton("-");
Subtract.setBounds(230,260,50,50);
Window.add(Subtract);
}

public static void main(String args[]){
new Frame();
}
}
vandale
  • 3,600
  • 3
  • 22
  • 39
andrew
  • 1
  • 2
    Indentation, naming conventions respect, swing threading rules, call to pack(), making the frame visible *after* you've put everything in it, layout managers. There are many, many things you're missing. All I said should give you hints. Read the swing tutorial about layout manager, and about concurrency. – JB Nizet Nov 18 '17 at 23:05
  • Thank you for the direction, I will read the documentation more thoroughly. – andrew Nov 18 '17 at 23:11
  • @JBNizet covered most of the important stuff, I'll just add.. See also this [calculator example](http://stackoverflow.com/a/7441804/418556). It uses `ScriptEngine` to evaluate the expression in the text field. (As an aside, the 'ready to run' code sample in that answer conforms to the advice outlined above.) – Andrew Thompson Nov 19 '17 at 01:42
  • Thank you for the input and example. I see now what a big difference a grid layout can make. – andrew Nov 26 '17 at 21:54
  • A follow up question is, in the calculator example, what lines of code add the key pad to the bottom of that frame? In other words make a jpanel that I define with gridlayout and an array of buttons then place that inside the jframe. In a way that this "key pad" is a method that is called and placed inside. So my actual UI method is clean and only calls and places it. I fear my words as a layman with be mistaken when read by a programmer. So please bear with me. – andrew Nov 27 '17 at 02:44

0 Answers0