Simple calculator using Java Frame
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class calculator extends Frame implements ActionListener
{
public static Boolean first,operatorflag,resultflag,equaltoflag;
public static double firstvalue;
public static String operator;
JButton btn_1,btn_2,btn_3,btn_4,btn_5,btn_6,btn_7,btn_8,btn_9;
import javax.swing.*;
import java.awt.event.*;
public class calculator extends Frame implements ActionListener
{
public static Boolean first,operatorflag,resultflag,equaltoflag;
public static double firstvalue;
public static String operator;
JButton btn_1,btn_2,btn_3,btn_4,btn_5,btn_6,btn_7,btn_8,btn_9;
JButton btn_0,btn_plus,btn_minus,btn_mul,btn_div,btn_equal,btn_clear;
JTextField txt_display;
public calculator()
{
first=false;
operator="";
operatorflag=false;
resultflag=false;
equaltoflag=false;
btn_1=new JButton("1");
btn_2=new JButton("2");
btn_3=new JButton("3");
btn_4=new JButton("4");
btn_5=new JButton("5");
btn_6=new JButton("6");
btn_7=new JButton("7");
btn_8=new JButton("8");
btn_9=new JButton("9");
btn_0=new JButton("0");
btn_plus=new JButton("+");
btn_minus=new JButton("-");
btn_mul=new JButton("*");
btn_div=new JButton("/");
btn_equal=new JButton("=");
btn_clear=new JButton("C");
txt_display=new JTextField(20);
txt_display.setEditable(false);
JTextField txt_display;
public calculator()
{
first=false;
operator="";
operatorflag=false;
resultflag=false;
equaltoflag=false;
btn_1=new JButton("1");
btn_2=new JButton("2");
btn_3=new JButton("3");
btn_4=new JButton("4");
btn_5=new JButton("5");
btn_6=new JButton("6");
btn_7=new JButton("7");
btn_8=new JButton("8");
btn_9=new JButton("9");
btn_0=new JButton("0");
btn_plus=new JButton("+");
btn_minus=new JButton("-");
btn_mul=new JButton("*");
btn_div=new JButton("/");
btn_equal=new JButton("=");
btn_clear=new JButton("C");
txt_display=new JTextField(20);
txt_display.setEditable(false);
JPanel jp=new JPanel();
jp.setLayout(new GridLayout(4,4));
jp.add(btn_1);
jp.add(btn_2);
jp.add(btn_3);
jp.add(btn_plus);
jp.add(btn_4);
jp.add(btn_5);
jp.add(btn_6);
jp.add(btn_minus);
jp.add(btn_7);
jp.add(btn_8);
jp.add(btn_9);
jp.add(btn_mul);
jp.add(btn_0);
jp.add(btn_clear);
jp.add(btn_equal);
jp.add(btn_div);
btn_1.addActionListener(this);
btn_2.addActionListener(this);
btn_3.addActionListener(this);
btn_4.addActionListener(this);
btn_5.addActionListener(this);
btn_6.addActionListener(this);
btn_7.addActionListener(this);
btn_8.addActionListener(this);
btn_9.addActionListener(this);
btn_0.addActionListener(this);
btn_plus.addActionListener(this);
btn_minus.addActionListener(this);
btn_mul.addActionListener(this);
btn_div.addActionListener(this);
btn_equal.addActionListener(this);
btn_clear.addActionListener(this);
this.setLayout(null);
this.add(txt_display);
this.add(jp);
txt_display.setBounds(20,50,200,50);
jp.setBounds(20,120,200,200);
}
public void actionPerformed(ActionEvent ae)
{
String n=ae.getActionCommand();
String number=txt_display.getText();
if(n.equals("0")||n.equals("1")||n.equals("2")||n.equals("3")||n.equals("4")||n.equals("5")||n.equals("6")||n.equals("7")||n.equals("8")||n.equals("9"))
{
if(resultflag==false)
{
number=number+n;
txt_display.setText(number);
}
else
{
txt_display.setText(n);
resultflag=false;
}
}
if(n.equals("C"))
{
txt_display.setText("");
first=false;
number="";
operator="";
operatorflag=false;
resultflag=false;
equaltoflag=false;
}
if(n.equals("+")||n.equals("-")||n.equals("*")||n.equals("/")||n.equals("="))
{
if(number.equals(""))
{
txt_display.setText("");
}
else
{
if(first==true)
{
double secondvalue;
secondvalue=Double.parseDouble(number);
double result;
result=0;
if(equaltoflag==false)
{
if(n.equals("+"))
{
result=firstvalue+secondvalue;
operator="+";
}
else if(n.equals("-"))
{
result=firstvalue-secondvalue;
operator="-";
}
else if(n.equals("*"))
{
result=firstvalue*secondvalue;
operator="*";
}
else if(n.equals("/"))
{
result=firstvalue/secondvalue;
operator="/";
}
}
else
{
txt_display.setText("");
equaltoflag=false;
operator=n;
result=Double.parseDouble(number);
firstvalue=Double.parseDouble(number);
}
if(n.equals("="))
{
if(operator.equals("+"))
result=firstvalue+secondvalue;
else if(operator.equals("-"))
result=firstvalue-secondvalue;
else if(operator.equals("*"))
result=firstvalue*secondvalue;
else if(operator.equals("/"))
result=firstvalue/secondvalue;
equaltoflag=true;
}
txt_display.setText(Double.toString(result));
firstvalue=result;
resultflag=true;
}
else if(first==false)
{
first=true;
firstvalue=Double.parseDouble(number);
txt_display.setText("");
operator=n;
}
}
}
}
public static void main(String args[])
{
calculator calc=new calculator();
calc.setSize(250,350);
calc.setTitle("Simple Calculator");
calc.setVisible(true);
calc.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}
Output
Simple Calculator in Java |
No comments:
Post a Comment