1

I'm trying to make a calculator for a school project and I can't get JTextArea to appear. I need a window so that the user can see the numbers they're inputting into the calculator and the output.

This is my code:

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

public class Calculator extends JFrame 
{

JButton[] nums = new JButton [10];

JButton num1 = new JButton ("1");
JButton num2 = new JButton ("2");
JButton num3 = new JButton ("3");
JButton num4 = new JButton ("4");
JButton num5 = new JButton ("5");
JButton num6 = new JButton ("6");
JButton num7 = new JButton ("7");
JButton num8 = new JButton ("8");
JButton num9 = new JButton ("9");
JButton num0 = new JButton ("0");
JButton decimal = new JButton (".");
JButton clear = new JButton ("C");
JButton sqrt = new JButton ("\u221A");
JButton mod = new JButton ("%");
JButton dividebyone = new JButton ("1/x");
JButton factorn = new JButton ("!n");

public Calculator ()
{
    JFrame window = new JFrame ("Calculator");
    setSize (260, 325);
    setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    setVisible (true);

    JTextArea screen = new JTextArea (20, 20);
    JScrollPane pls = new JScrollPane (screen);
    screen.setBackground (Color.WHITE);
    screen.setVisible (true);
    getContentPane ().add (pls);

    getContentPane ().add (stylize (num1));
    num1.setBounds (20, 75, 45, 45);
    num1.addActionListener (new ActionListener ()
    {

        public void actionPerformed (ActionEvent e)
        {
            //Execute when button is pressed
            System.out.println ("1");
        }
    }
    );

    getContentPane ().add (stylize (num2));
    num2.setBounds (70, 75, 45, 45);
    num2.addActionListener (new ActionListener ()
    {

        public void actionPerformed (ActionEvent e)
        {
            System.out.println ("2");
        }
    }
    );

    getContentPane ().add (stylize (num3));
    num3.setBounds (120, 75, 45, 45);
    num3.addActionListener (new ActionListener ()
    {

        public void actionPerformed (ActionEvent e)
        {
            System.out.println ("3");
        }
    }
    );

    getContentPane ().add (stylize (num4));
    num4.setBounds (20, 125, 45, 45);
    num4.addActionListener (new ActionListener ()
    {

        public void actionPerformed (ActionEvent e)
        {
            System.out.println ("4");
        }
    }
    );

    getContentPane ().add (stylize (num5));
    num5.setBounds (70, 125, 45, 45);
    num5.addActionListener (new ActionListener ()
    {

        public void actionPerformed (ActionEvent e)
        {
            System.out.println ("5");
        }
    }
    );

    getContentPane ().add (stylize (num6));
    num6.setBounds (120, 125, 45, 45);
    num6.addActionListener (new ActionListener ()
    {

        public void actionPerformed (ActionEvent e)
        {
            System.out.println ("6");
        }
    }
    );

    getContentPane ().add (stylize (num7));
    num7.setBounds (20, 175, 45, 45);
    num7.addActionListener (new ActionListener ()
    {

        public void actionPerformed (ActionEvent e)
        {
            System.out.println ("7");
        }
    }
    );

    getContentPane ().add (stylize (num8));
    num8.setBounds (70, 175, 45, 45);
    num8.addActionListener (new ActionListener ()
    {

        public void actionPerformed (ActionEvent e)
        {
            System.out.println ("8");
        }
    }
    );

    getContentPane ().add (stylize (num9));
    num9.setBounds (120, 175, 45, 45);
    num9.addActionListener (new ActionListener ()
    {

        public void actionPerformed (ActionEvent e)
        {
            System.out.println ("9");
        }
    }
    );

    getContentPane ().add (stylize (num0));
    num0.setBounds (20, 225, 45, 45);
    num0.addActionListener (new ActionListener ()
    {

        public void actionPerformed (ActionEvent e)
        {
            System.out.println ("0");
        }
    }
    );

    getContentPane ().add (stylize (decimal));
    decimal.setBounds (70, 225, 45, 45);
    decimal.addActionListener (new ActionListener ()
    {

        public void actionPerformed (ActionEvent e)
        {
            System.out.println (".");
        }
    }
    );

    getContentPane ().add (stylize (clear));
    clear.setBounds (120, 225, 45, 45);
    clear.addActionListener (new ActionListener ()
    {

        public void actionPerformed (ActionEvent e)
        {
            System.out.println ("c");
        }
    }
    );

    getContentPane ().add (stylize (sqrt));
    sqrt.setBounds (170, 75, 51, 45);
    sqrt.addActionListener (new ActionListener ()
    {

        public void actionPerformed (ActionEvent e)
        {
            System.out.println ("sqrt");
        }
    }
    );

    getContentPane ().add (stylize (mod));
    mod.setBounds (170, 125, 51, 45);
    mod.addActionListener (new ActionListener ()
    {

        public void actionPerformed (ActionEvent e)
        {
            System.out.println ("mod");
        }
    }
    );

    getContentPane ().add (stylize (factorn));
    factorn.setBounds (170, 175, 51, 45);
    factorn.addActionListener (new ActionListener ()
    {

        public void actionPerformed (ActionEvent e)
        {
            System.out.println ("!n");
        }
    }
    );

    getContentPane ().add (stylize (dividebyone));
    dividebyone.setBounds (170, 225, 51, 45);
    dividebyone.addActionListener (new ActionListener ()
    {

        public void actionPerformed (ActionEvent e)
        {
            System.out.println ("1/x");
        }
    }
    );

} //end Calculator


public static JButton stylize (JButton button)
{
    button.setBackground (Color.WHITE);
    return button;
} //end stylize


public static void main (String args[])
{
    new Calculator ();
} //end main
} //end Calculator class
randominstanceOfLivingThing
  • 16,873
  • 13
  • 49
  • 72
trachea
  • 11
  • 2
  • If you are using en ide like eclipse make sure you select all the text using CTRL+A and then format it using CTRL+SHIFT+F so that code is formatted well. – randominstanceOfLivingThing Apr 29 '16 at 15:16
  • you could try adding all of your components to a panel, and then set that panel as the content using the `setContentPane(Container yourNewPanel)` – iestync Apr 29 '16 at 15:16
  • 1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) See also this [calculator example](http://stackoverflow.com/a/7441804/418556). It uses `ScriptEngine` to evaluate the expression in the text field. – Andrew Thompson Apr 29 '16 at 15:49

2 Answers2

0

Try doing this :

Public static void main(String arts[])
{
    Calculator c= new Calculator();
    c.setVisible(true);
}

Remove setVisible(true) from constructor. Hope this works.
As your Frame is being visible before you're adding components .So it is not displaying your components.

Abhishek Panjabi
  • 439
  • 4
  • 23
  • It looks like there isn't a layout manager so the positioning is absolute, try setting a size for the scrollPane e.g. `pls.setBounds(20, 10, 200, 45);` it would be worth reading a bit about layout managers too if you get a chance – iestync Apr 29 '16 at 15:34
0

something like this should display the text area given the code you've started and taking @Abhishek's advice

public class Calculator extends JFrame {

    JButton[] nums = new JButton[10];

    JButton num1 = new JButton("1");
    JButton num2 = new JButton("2");
    JButton num3 = new JButton("3");
    JButton num4 = new JButton("4");
    JButton num5 = new JButton("5");
    JButton num6 = new JButton("6");
    JButton num7 = new JButton("7");
    JButton num8 = new JButton("8");
    JButton num9 = new JButton("9");
    JButton num0 = new JButton("0");
    JButton decimal = new JButton(".");
    JButton clear = new JButton("C");
    JButton sqrt = new JButton("\u221A");
    JButton mod = new JButton("%");
    JButton dividebyone = new JButton("1/x");
    JButton factorn = new JButton("!n");

    public Calculator() {
        setSize(260, 325);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final JPanel content = new JPanel();
        content.setLayout(null);

        JTextArea screen = new JTextArea();
        JScrollPane pls = new JScrollPane(screen);
        pls.setBounds(20, 10, 200, 45);
        screen.setBackground(Color.WHITE);
        content.add(pls);

        content.add(stylize(num1));
        num1.setBounds(20, 75, 45, 45);
        num1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                // Execute when button is pressed
                System.out.println("1");
            }
        });

        content.add(stylize(num2));
        num2.setBounds(70, 75, 45, 45);
        num2.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.out.println("2");
            }
        });

        content.add(stylize(num3));
        num3.setBounds(120, 75, 45, 45);
        num3.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.out.println("3");
            }
        });

        content.add(stylize(num4));
        num4.setBounds(20, 125, 45, 45);
        num4.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.out.println("4");
            }
        });

        content.add(stylize(num5));
        num5.setBounds(70, 125, 45, 45);
        num5.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.out.println("5");
            }
        });

        content.add(stylize(num6));
        num6.setBounds(120, 125, 45, 45);
        num6.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.out.println("6");
            }
        });

        content.add(stylize(num7));
        num7.setBounds(20, 175, 45, 45);
        num7.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.out.println("7");
            }
        });

        content.add(stylize(num8));
        num8.setBounds(70, 175, 45, 45);
        num8.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.out.println("8");
            }
        });

        content.add(stylize(num9));
        num9.setBounds(120, 175, 45, 45);
        num9.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.out.println("9");
            }
        });

        content.add(stylize(num0));
        num0.setBounds(20, 225, 45, 45);
        num0.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.out.println("0");
            }
        });

        content.add(stylize(decimal));
        decimal.setBounds(70, 225, 45, 45);
        decimal.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.out.println(".");
            }
        });

        content.add(stylize(clear));
        clear.setBounds(120, 225, 45, 45);
        clear.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.out.println("c");
            }
        });

        content.add(stylize(sqrt));
        sqrt.setBounds(170, 75, 51, 45);
        sqrt.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.out.println("sqrt");
            }
        });

        content.add(stylize(mod));
        mod.setBounds(170, 125, 51, 45);
        mod.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.out.println("mod");
            }
        });

        content.add(stylize(factorn));
        factorn.setBounds(170, 175, 51, 45);
        factorn.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.out.println("!n");
            }
        });

        content.add(stylize(dividebyone));
        dividebyone.setBounds(170, 225, 51, 45);
        dividebyone.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.out.println("1/x");
            }
        });
        setContentPane(content);
    } // end Calculator

    public static JButton stylize(JButton button) {
        button.setBackground(Color.WHITE);
        return button;
    } // end stylize

    public static void main(String args[]) {
        final Calculator calc = new Calculator();
        calc.setVisible(true);
    } // end main
} // end Calculator class
iestync
  • 121
  • 6
  • *"something like this should display the text area"* Given `content.setLayout(null);` it will be very fragile, and likely break on the next machine. – Andrew Thompson Apr 29 '16 at 15:50
  • Ye it would be better to use a grid based layout manager for this, can it really break depending on the machine? – iestync Apr 29 '16 at 16:33
  • *"can it really break depending on the machine?"* Let me think - things that might affect it. Screen size, screen resolution, the brand (or version) of the JRE, the OS, the PLAF, the default fonts.. So perhaps not 'machine' so much, but a lot of other factors over which the programmer has no, or very little, control. – Andrew Thompson Apr 29 '16 at 16:56
  • thanks for following up on that, ye these reasons all make sense, will keep these in mind going forward – iestync May 01 '16 at 11:08