0
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GUI extends JFrame
{
    //Setting out the variables
    JTextField answerResponse;
    JButton one,two,three,four,five,six,seven,eight,nine,zero,add,subtract,multiply,divide,equals;
    String var1, var2, tanswer;
    Double answer;
    JPanel contentpanel;
    public GUI()
    {
        //JFrame Window
        JFrame frame = new JFrame();
        frame.setSize(250, 400);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setVisible(true);
        frame.setTitle("Calculator");
        frame.setLocationRelativeTo(null);

        //Buttons
        one = new JButton("1"); two = new JButton("2"); three = new JButton("3");
        four = new JButton("4"); five = new JButton("5"); six = new JButton("6");
        seven = new JButton("7"); eight = new JButton("8"); nine = new JButton("9");
        zero = new JButton("0"); add = new JButton("+"); divide = new JButton("/");
        multiply = new JButton("*"); subtract = new JButton("-"); equals = new JButton("=");

        //Dimensions
        Dimension dim = new Dimension(75, 25);
        one.setPreferredSize(dim); two.setPreferredSize(dim); three.setPreferredSize(dim);
        four.setPreferredSize(dim); five.setPreferredSize(dim); six.setPreferredSize(dim);
        seven.setPreferredSize(dim); eight.setPreferredSize(dim); nine.setPreferredSize(dim);
        zero.setPreferredSize(new Dimension(225, 25)); add.setPreferredSize(new Dimension(113, 25)); subtract.setPreferredSize(new Dimension(113, 25)); 
        multiply.setPreferredSize(new Dimension(113, 25)); divide.setPreferredSize(new Dimension(113, 25)); equals.setPreferredSize(new Dimension(225, 25));

        //Content Panel
        JPanel contentPanel = new JPanel();
        frame.setContentPane(contentPanel);
        contentPanel.setLayout(new FlowLayout());

        //Adding to content panel
        contentPanel.add(one); contentPanel.add(two); contentPanel.add(three);
        contentPanel.add(four); contentPanel.add(five); contentPanel.add(six);
        contentPanel.add(seven); contentPanel.add(eight); contentPanel.add(nine);
        contentPanel.add(zero); contentPanel.add(add); contentPanel.add(subtract);
        contentPanel.add(multiply); contentPanel.add(divide); contentPanel.add(equals);

        //Declaring Variable Values
        answerResponse = new JTextField(null, 20);
        answerResponse.setEditable(false);
        contentPanel.add(answerResponse);
    }
}

I am having a problem while trying to make my calculator. The text field called "answerResponse" is not showing up. The rest of the buttons and such are, the text field is supposed to be the first thing at the top.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Michael
  • 39
  • 3

1 Answers1

2

Take frame.setVisible(true); and place it below contentPanel.add(answerResponse); as the last.

While there's no evidence to suggest otherwise, make sure you are executing your UI code within the context of the Event Dispatching Thread, see Initial Threads for more details

Also have a look at Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?

You should also get use to not using setSize of windows and use pack instead, as this will take into consideration the difference that occur between platforms and the windows borders for various look and feels

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366