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