My program does not display the JTextField portion of the calculator, and the clear JButton won't work to reset the calculator, so when I clear the button and try to perform a new calculation, I get a runtime error.BTW the Boolean s is supposed to be operationchosen
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class calculator extends JFrame
{
private JButton num1;
private JButton num2;
private JButton num3;
private JButton num4;
private JButton num5;
private JButton num6;
private JButton num7;
private JButton num8;
private JButton num9;
private JButton num0;
private JButton equal;
private JButton addition;
private JButton sub;
private JButton mult;
private JButton divide;
private JButton modulus;
private JButton solve;
private JButton clear;
private boolean s=false;
private boolean equalsClicked=false;
private double val;
private String temp1,temp2,answer;
private JTextField answerfield;
private JPanel contentPane;
private char operation=' ';
public calculator(String x)
{
super(x);
answerfield=new JTextField();
answerfield.setEditable(false);
answerfield.setPreferredSize(new Dimension(160, 20));
answerfield.setBackground(Color.WHITE);
answerfield.setEnabled(false);
answerfield.setHorizontalAlignment(4);
answerfield.setDisabledTextColor(Color.BLACK);
contentPane = new JPanel();
contentPane.setLayout(null);
contentPane.add(answerfield,BorderLayout.NORTH);
setBounds(400,300,255,400);
this.setContentPane(contentPane);
Numbers n=new Numbers();
Calculation c=new Calculation();
num1=new JButton("1");
num1.setBounds(10, 130, 52, 37);
add(num1);
num1.setActionCommand("1");
num1.addActionListener(n);
num2=new JButton("2");
num2.setBounds(72, 130, 52, 37);
add(num2);
num2.setActionCommand("2");
num2.addActionListener(n);
num3=new JButton("3");
num3.setBounds(134, 130, 52, 37);
add(num3);
num3.setActionCommand("3");
num3.addActionListener(n);
num4=new JButton("4");
num4.setBounds(10, 178, 52, 37);
add(num4);
num4.setActionCommand("4");
num4.addActionListener(n);
num5=new JButton("5");
num5.setBounds(72, 178, 52, 37);
add(num5);
num5.setActionCommand("5");
num5.addActionListener(n);
num6=new JButton("6");
num6.setBounds(134, 178, 52, 37);
add(num6);
num6.setActionCommand("6");
num6.addActionListener(n);
num7=new JButton("7");
num7.setBounds(10, 226, 52, 37);
add(num7);
num7.setActionCommand("7");
num7.addActionListener(n);
num8=new JButton("8");
num8.setBounds(72, 226, 52, 37);
add(num8);
num8.setActionCommand("8");
num8.addActionListener(n);
num9=new JButton("9");
num9.setBounds(134, 226, 52, 37);
add(num9);
num9.setActionCommand("9");
num9.addActionListener(n);
num0=new JButton("0");
num0.setBounds(10, 274, 52, 37);
add(num0);
num0.setActionCommand("0");
num0.addActionListener(n);
equal=new JButton("=");
equal.setBounds(72, 274, 52, 37);
add(equal);
equal.setActionCommand("=");
equal.addActionListener(c);
addition=new JButton("+");
addition.setBounds(193, 130,45, 35);
add(addition);
addition.setActionCommand("+");
addition.addActionListener(c);
sub=new JButton("-");
sub.setBounds(193, 179, 45, 35);
add(sub);
sub.setActionCommand("-");
sub.addActionListener(c);
mult=new JButton("*");
mult.setBounds(193,226, 45, 35);
add(mult);
mult.setActionCommand("*");
mult.addActionListener(c);
divide=new JButton("/");
divide.setBounds(193, 274, 45, 35);
add(divide);
divide.setActionCommand("/");
divide.addActionListener(c);
modulus=new JButton("%");
modulus.setBounds(193,322, 45, 35);
add(modulus);
modulus.setActionCommand("%");
modulus.addActionListener(c);
clear=new JButton("C");
clear.setBounds(53, 343, 89, 23);
add(clear);
clear.setActionCommand("C");
clear.addActionListener(c);
setVisible(true);
show();
setResizable(false);
}
private class Numbers implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
JButton x=(JButton)event.getSource();
if(x.equals(num1))
{
if(s==false)
{
if(temp1==null)
{
temp1="1";
}
else
temp1+="1";
}
else
{
if(temp2==null)
{
temp2="1";
}
else
temp2+="1";
}
}
if(x.equals(num2))
{
if(s==false)
{
if(temp1==null)
{
temp1="2";
}
else
temp1+="2";
}
else
{
if(temp2==null)
{
temp2="2";
}
else
temp2+="2";
}
}
if(x.equals(num3))
{
if(s==false)
{
if(temp1==null)
{
temp1="3";
}
else
temp1+="3";
}
else
{
if(temp2==null)
{
temp2="3";
}
else
temp2+="3";
}
}
if(x.equals(num4))
{
if(s==false)
{
if(temp1==null)
{
temp1="4";
}
else
temp1+="4";
}
else
{
if(temp2==null)
{
temp2="4";
}
else
temp2+="4";
}
}
if(x.equals(num5))
{
if(s==false)
{
if(temp1==null)
{
temp1="5";
}
else
temp1+="5";
}
else
{
if(temp2==null)
{
temp2="5";
}
else
temp2+="5";
}
}
if(x.equals(num6))
{
if(s==false)
{
if(temp1==null)
{
temp1="6";
}
else
temp1+="6";
}
else
{
if(temp2==null)
{
temp2="6";
}
else
temp2+="6";
}
}
if(x.equals(num7))
{
if(s==false)
{
if(temp1==null)
{
temp1="7";
}
else
temp1+="7";
}
else
{
if(temp2==null)
{
temp2="7";
}
else
temp2+="7";
}
}
if(x.equals(num8))
{
if(s==false)
{
if(temp1==null)
{
temp1="8";
}
else
temp1+="8";
}
else
{
if(temp2==null)
{
temp2="8";
}
else
temp2+="8";
}
}
if(x.equals(num9))
{
if(s==false)
{
if(temp1==null)
{
temp1="9";
}
else
temp1+="9";
}
else
{
if(temp2==null)
{
temp2="9";
}
else
temp2+="9";
}
}
if(x.equals(num0))
{
if(s==false)
{
if(temp1==null)
{
temp1="0";
}
else
temp1+="0";
}
else
{
if(temp2==null)
{
temp2="0";
}
else
temp2+="0";
}
}
if(equalsClicked==false)
{
if(s==false)
{
answerfield.setText(temp1);
}
else
{
answerfield.setText(temp2);
}
}
}
}
public void Clear()
{
temp1="";
temp2="";
s=false;
operation=' ';
val=0;
answer="";
equalsClicked=false;
System.out.println("hey");
}
private class Calculation implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
JButton x=(JButton)event.getSource();
if(x.equals(addition))
{
if(temp1==null)
System.out.println("choose numbers");
else
{
if(temp1!=null&&temp2==null)
{
s=true;
operation='+';
}
}
}
if(x.equals(sub))
{
if(temp1==null)
System.out.println("choose numbers");
else
{
if(temp1!=null&&temp2==null)
{
s=true;
operation='-';
}
}
}
if(x.equals(mult))
{
if(temp1==null)
System.out.println("choose numbers");
else
{
if(temp1!=null&&temp2==null)
{
s=true;
operation='*';
}
}
}
if(x.equals(divide))
{
if(temp1==null)
System.out.println("choose numbers");
else
{
if(temp1!=null&&temp2==null)
{
s=true;
operation='/';
}
}
}
if(x.equals(modulus))
{
if(temp1==null)
System.out.println("choose numbers");
else
{
if(temp1!=null&&temp2==null)
{
s=true;
operation='%';
}
}
}
if(x.equals(equal))
{
if(temp1==null)
System.out.println("choose numbers");
else
{
if(temp1!=null&&temp2==null)
System.out.println("choose numbers");
}
if(temp1!=null&&temp2!=null)
{
double d=0.0;
double s=0.0;
d=Double.parseDouble(temp1);
s=Double.parseDouble(temp2);
switch(operation)
{
case '+':
{
val=d+s;
break;
}
case '-':
{
val=d-s;
break;
}
case'*':
{
val=d*s;
break;
}
case'/':
{
val=d/s;
break;
}
case'%':
{
val=d%s;
break;
}
}
answer=Double.toString(val);
answerfield.setText(answer);
System.out.println(answer);
}
}
if(x.equals(clear))
{
Clear();
}
}
}
public static void main(String args[])
{
new calculator("math");
}
}