A Java-implemented calculator applet

Source: Internet
Author: User

/**


@author Song Liang Peng


@version 2005.7.23


*/


import java.awt.*;


import java.awt.event.*;


public class Counter extends Frame implements ActionListener


{


TextField t=new TextField ("", 15);


Panel p1=new Panel ();


Panel p2=new Panel ();


button[] B=new button[10];


button Badd=new button ("+");


button Bsub=new button ("-");


button Bmul=new button ("*");


button Bdiv=new button ("/");


button Bpoint=new button (".");


button Bequal=new button ("=");


button Bsqrt=new button ("Open square");


button Bnull=new button ("Empty");


String str1= ""; STR1 and str2 store two input numbers


String str2= "";


String Operator=null; A symbol for storing subtraction and opening squares


Boolean first=true; Verify that the input is the first number


int countoper=0; Cumulative number of input symbols, even in addition to the operation will be used to


double result=0.0; Staging Results


double num1=0.0,num2=0.0; Two input numbers are converted to double storage when doing operations


Boolean error=false; Check whether the divisor is 0


//Construction method


public Counter ()


{


Super ("counter");


t.seteditable (FALSE);


for (int i=0;i<b.length;i++)


 {


b[i]=new Button (string.valueof (i));


P1.add (B[i]);


B[i].setactioncommand ("number");


B[i].addactionlistener (this);


 }


P1.add (Bpoint);


Bpoint.setactioncommand ("number");


P1.add (Badd); Number keys, symbol keys placed in the P1 of the panel


P1.add (bsub);


P1.add (Bmul);


P1.add (BDIV);


P1.add (bequal);


P2.add (BSQRT); The square and empty keys are placed in the P2 of the panel


P2.add (bnull);


Badd.setactioncommand ("Oper");


Bsub.setactioncommand ("oper");


Bmul.setactioncommand ("oper");


Bdiv.setactioncommand ("oper");


Badd.addactionlistener (this);


Bsub.addactionlistener (this);


Bmul.addactionlistener (this);


Bdiv.addactionlistener (this);


Bpoint.addactionlistener (this);


Bequal.addactionlistener (this);


Bsqrt.addactionlistener (this);


Bnull.addactionlistener (this);


p1.setlayout (New GridLayout (4,4,5,5));


p2.setlayout (New FlowLayout ());


Add (T, "North"); Frame's North Place input box, the panel is placed in center and South


Add (P1, "Center");


Add (P2, "South");


setlocation (400,200);


setSize (200,200);


SetBackground (color.red);


setvisible (TRUE);


Addwindowlistener (New Windowadapter () {//close window


public void windowclosing (WindowEvent e)


  {


system.exit (0);


  }


 });


}


//Implementation interface ActionListener


public void actionperformed (ActionEvent e)


{


button temp= (button) E.getsource ();


if (E.getactioncommand (). Equals ("number"))


 {


if (a)


  {


Str1=str1+temp.getlabel ();


T.settext (STR1);


  }


Else


  {


Str2=str2+temp.getlabel ();


T.settext (STR2);


  }


 }


Else if (E.getactioncommand (). Equals ("oper"))


 {


if (str1== "")///If no input is entered, click Operator to perform if,127 line of empathy


  {


countoper=0;


first=true;


  }


Else


  {


countoper++;


if (countoper>1)


  {


GetResult ();


  }


Operator=temp.getlabel ();


First=false;


  }


 }


Else if (E.getactioncommand (). Equals ("Open square")


 {


if (str1== "")


  {


countoper=0;


first=true;


  }


Else


  {


countoper=1;


if (countoper>1)


  {


GetResult ();


  }


Double d=math.sqrt (double.parsedouble (str1));


str1=string.valueof (d);


T.settext (string.valueof (d));


First=false;


  }


 }


Else if (E.getactioncommand (). Equals ("Empty")


 {


str1= "";


str2= "";


T.settext ("");


countoper=0;


first=true;


Error=false;


 }


Else if (E.getactioncommand (). Equals ("="))


 {


if ((str1== "") | |  (str2== "")) Two numbers without losing all click on the equals sign to perform if


  {


countoper=0;


first=true;


  }


Else


  {


GetResult ();


countoper=0;


  }


 }


}


//Calculation results of the method


public void GetResult ()


{


num1=double.parsedouble (STR1);


num2=double.parsedouble (STR2);


if (operator.equals ("+"))


 {


result=num1+num2;


 }


Else if (operator.equals ("-"))


 {


result=num1-num2;


 }


Else if (operator.equals ("*"))


 {


result=num1*num2;


 }


Else if (operator.equals ("/"))


 {


if (num2==0.0)//Divisor 0 processing method


  {


error=true;


  }


Else


  {


result=num1/num2;


  }


 }


if (Error)


 {


t.settext ("error");


 }


Else


 {


T.settext (string.valueof (result));


str1=string.valueof (Result); After the operation of the results into the str1, str2 empty, in addition to the operation of continuous reduction to prepare


str2= "";


 }


}


//Main method


public static void Main (string[] args)


{


new Counter ();


}


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.