Import Java. AWT .*;
Import Java. AWT. event .*;
Import Javax. Swing .*;
Public Class Calculator Implements Actionlistener {
Jframe JF = New Jframe ("Calculator by chenyadong ");
Jtextfield TF = New Jtextfield (); // Area of the input number
Double Data; // Records the data before the input symbol.
String operator; // Symbol bit
Boolean Clickable = True , Clear = True ; // Clickable is used to determine "." And clear is used to determine whether to click the symbol bit to clear the screen.
Public Void Init (){
JF. setsize (300,200 );// SET SIZE
JF. setvisible ( True ); // Set to visible
Container c = JF. getcontentpane (); // Get the content pane object of jframe.
TF. sethorizontalalignment (jtextfield. Right ); // Right alignment
C. Add (TF, "North ");
Jpanel pn1 = New Jpanel ( New Gridlayout (4, 4 )); // Operation Interface
C. Add (pn1, "center ");
Jbutton B = New Jbutton ("7 ");
B. addactionlistener ( This );
Pn1.add (B );
B = New Jbutton ("8 ");
B. addactionlistener (This );
Pn1.add (B );
B = New Jbutton ("9 ");
B. addactionlistener ( This );
Pn1.add (B );
B = New Jbutton ("/");
B. addactionlistener ( This );
Pn1.add (B );
B = New Jbutton ("4 ");
B. addactionlistener ( This );
Pn1.add (B );
B = New Jbutton ("5 ");
B. addactionlistener ( This );
Pn1.add (B );
B = New Jbutton ("6 ");
B. addactionlistener ( This );
Pn1.add (B );
B = New Jbutton ("*");
B. addactionlistener ( This );
Pn1.add (B );
B = New Jbutton ("1 ");
B. addactionlistener ( This );
Pn1.add (B );
B = New Jbutton ("2 ");
B. addactionlistener ( This );
Pn1.add (B );
B = New Jbutton ("3 ");
B. addactionlistener ( This );
Pn1.add (B );
B = New Jbutton ("-");
B. addactionlistener ( This );
Pn1.add (B );
B = New Jbutton ("0 ");
B. addactionlistener ( This );
Pn1.add (B );
B = New Jbutton (".");
B. addactionlistener ( This );
Pn1.add (B );
B =New Jbutton ("= ");
B. addactionlistener ( This );
Pn1.add (B );
B = New Jbutton ("+ ");
B. addactionlistener ( This );
Pn1.add (B );
}
Public Void Actionreceivmed (actionevent e ){
// Todo auto-generated method stub
String temp = E. getactioncommand (); // Get button tag
If (Temp = "0 "){
If (Clear = False ){ // Determine whether the symbol bit is clicked
TF. settext ("");
}
TF. settext (Tf. gettext () + "0 ");
}
If (Temp = "1 "){
If (Clear = False ){
TF. settext ("");
}
TF. settext (Tf. gettext () + "1 ");
Clear = True ;
}
If (Temp = "1 "){
If (Clear = False ){
TF. settext ("");
}
TF. settext (Tf. gettext () + "1 ");
Clear = True ;
}
If (Temp = "2 "){
If (Clear = False ){
TF. settext ("");
}
TF. settext (Tf. gettext () + "2 ");
Clear = True ;
}
If (Temp = "3 "){
If (Clear = False ){
TF. settext ("");
}
TF. settext (Tf. gettext () + "3 ");
Clear = True ;
}
If (Temp = "4 "){
If (Clear = False ){
TF. settext ("");
}
TF. settext (Tf. gettext () + "4 ");
Clear = True ;
}
If (Temp = "5 "){
If (Clear = False ){
TF. settext ("");
}
TF. settext (Tf. gettext () + "5 ");
Clear = True ;
}
If (Temp = "6 "){
If (Clear =False ){
TF. settext ("");
}
TF. settext (Tf. gettext () + "6 ");
Clear = True ;
}
If (Temp = "7 "){
If (Clear = False ){
TF. settext ("");
}
TF. settext (Tf. gettext () + "7 ");
Clear = True ;
}
If (Temp = "8 "){
If (Clear = False ){
TF. settext ("");
}
TF. settext (Tf. gettext () + "8 ");
Clear = True ;
}
If (Temp = "9 "){
If (Clear = False ){
TF. settext ("");
}
TF. settext (Tf. gettext () + "9 ");
Clear = True ;
}
If (Temp = "."){
Clickable = True ;
For ( Int I = 0; I <TF. gettext (). Length (); I ++ ){ // Traverse to determine if "." already exists "."
If (Tf. gettext (). charat (I) = '.'){
Clickable = False ;
Break ;
}
}
If (Clickable = True ){
TF. settext (Tf. gettext () + ".");
}
}
Try {
If (Temp = "+ "){
Data = double. parsedouble (Tf. gettext ()); // String parsed to double type
Operator = "+ ";
Clear = False ;
}
If (Temp = "-"){
Data = double. parsedouble (Tf. gettext ());
Operator = "-";
Clear = False ;
}
If (Temp = "*"){
Data = double. parsedouble (Tf. gettext ());
Operator = "*";
Clear = False ;
}
If (Temp = "/"){
Data = double. parsedouble (Tf. gettext ());
Operator = "/";
Clear = False ;
}
If (Temp = "= "){
Double Data2 = double. parsedouble (Tf. gettext ());
TF. settext ("");
If (Operator = "+ ")
TF. settext (Data + data2 + ""); // + "" To string type
If (Operator = "-")
TF. settext (data-data2 + "");
If (Operator = "*")
TF. settext (Data * data2 + "");
If (Operator = "/")
TF. settext (data/data2 + "");
Clear = False ;
}
} Catch (Exception ee ){
System. Out. println ("During calculation, enter the number first ");
Clear = False ;
}
}
Public Static Void Main (string ARGs []) {
New Calculator (). INIT ();
}
}