Java calculator source program

Source: Internet
Author: User

Import java. AWT .*;
Import javax. Swing .*;
Import java. AWT. event .*;
Class calculation extends jframe
{Public calculation ()/* constructor */
{Super ("counter ");
This. setdefaclocloseoperation (jframe. exit_on_close );
Inittextpanel ();/* text box */
Initcontrolpanel ();/* control key */
Initkeypanel ();/* Numbers and operators */
Container pane = getcontentpane ();
Pane. setlayout (New borderlayout ());
Pane. Add (textpanel, borderlayout. North );
Pane. Add (controlpanel, borderlayout. center );
Pane. Add (keypanel, borderlayout. South );
Pack ();
Try
{Uimanager. setlookandfeel ("com. Sun. java. Swing. plaf. Windows. windowslookandfeel ");
}
Catch (exception ex)
{;}/* Set the view of windons */
Swingutilities. updatecomponenttreeui (this );
Setresizable (false );
Setvisible (true );
}

Private void inittextpanel ()/* set text box */
{Textpanel = new jpanel ();
Textpanel. setlayout (New flowlayout ());
Resultarea = new jtextfield ("0", 25 );
Resultarea. seteditable (false);/* set to uneditable */
Color color = color. White;
Resultarea. setbackground (color);/* color */
Resultarea. sethorizontalalignment (jtextfield. Right);/* set the display position */
Textpanel. Add (resultarea );
}

Private void initcontrolpanel ()/* set the control key */
{Controlpanel = new jpanel ();
Controlpanel. setlayout (New gridlayout (1, 3, 4 ));
Jbutton b1 = new jbutton ("backspace");/* backspace key */
B1.setfont (font1 );
B1.addactionlistener (New actionlistener ()
{Public void actionreceivmed (actionevent E)
{String S1 = resultarea. gettext ();
Int L = s1.length ();
Resultarea. settext (s1.substring (0 L-1 ));
}
});

Controlpanel. Add (B1 );
Jbutton b2 = new jbutton ("CE");/* ce key */
B2.setfont (font1 );
B2.addactionlistener (New actionlistener ()
{Public void actionreceivmed (actionevent E)
{Resultarea. settext ("0 ");
Isnew = true;
}
});
Controlpanel. Add (B2 );

Jbutton B3 = new jbutton ("C");/* C key */
B3.setfont (font1 );
B3.addactionlistener (New actionlistener ()
{Public void actionreceivmed (actionevent E)
{Resultarea. settext ("0 ");
Pnum = "";
Operation = "";
Isnew = true;
}
});
Controlpanel. Add (B3 );
}

Private void initkeypanel ()/* set the number key and operator key */
{String key [] = {"7", "8", "9", "/", "SQRT", "4", "5", "6 ", "*", "%", "1", "2", "3", "-", "1/X", "0", "+ /-", ". "," + "," = "};

Keypanel = new jpanel ();
Keypanel. setlayout (New gridlayout (4,5, 4,4 ));
For (INT I = 0; I <20; I ++)
{String label = Key [I];
Jbutton B = new jbutton (Label );
B. setactioncommand (Key [I]);
B. setfont (font2 );
Keypanel. Add (B );
B. addactionlistener (New actionlistener ()/* anonymous listener */
{Public void actionreceivmed (actionevent E)
{Key_actionreceivmed (E );
}
});
}
}
Public void key_actionreceivmed (actionevent E)/* number key and operator key anonymous listener */
{String S = (E. getactioncommand ());
String ST = resultarea. gettext ();
If (S. Equals ("0")/* The number of inputs is 0 */
{If (St. Equals ("0 "))
Return;
Else
{If (! Isnew)
Resultarea. settext (ST + "0 ");
Else
Resultarea. settext ("0 ");
}
Isnew = false;
Return;
}

If (S. Equals ("+/-")/* The number of inputs is + /-*/
{Double K = double. parsedouble (ST );
{If (K! = 0)
Display (-k );

}
Return;
}

If (S. equals ("1") | S. equals ("2") | S. equals ("3") | S. equals ("4") | S. equals ("5") | S. equals ("6") | S. equals ("7") | S. equals ("8") | S. equals ("9")/* input 1-9 */

{If (! Isnew)
Resultarea. settext (ST + S );
Else
{Resultarea. settext (s );
Isnew = false;
}
Return;
}

If (S. Equals (".")/* enter the decimal point */
{If (resultarea. gettext (). indexof (".") =-1)/* No decimal point */
{If (isnew)
{Resultarea. settext ("0 .");
Isnew = false;
}
Else
Resultarea. settext (ST + ".");
}
Return;
}

Isnew = true;/* press the operator and enter a new number */
If (S. Equals ("= "))
{Compute (s );
Operation = "";
}
Else
{If (S. equals ("+") | S. equals ("-") | S. equals ("*") | S. equals ("/")/* binary operator number */

{If (operation. Equals (""))
{Pnum = resultarea. gettext ();
Operation = s;
}
Else
Compute (s );
}
Else/* maxcompute */
{Count count1 = new count (double. parsedouble (ST ));
If (S. Equals ("SQRT "))
{
Display (count1.sqrt ());

}
Else
{If (S. Equals ("1/X "))
{If (St. Equals ("0 "))
{Resultarea. settext ("the divisor cannot be 0 .");
Operation = "";
Pnum = "";
}
Else
Display (count1.reciprocal ();/* calculate the reciprocal */
}
Else
Display (double. parsedouble (ST)/100);/* enter % to divide the currently displayed value to 100 */

}
}
}
}

Private void compute (string S)
{If (S. Equals ("= "))
{If (operation. Equals (""))
Return;
}
Double data1 = double. parsedouble (pnum );
Double data2 = double. parsedouble (resultarea. gettext ());
Count count2 = new count (data1, data2);/* addition, subtraction, multiplication, division, and calculation */
If (operation. Equals ("+ "))
Display (count2.plus ()));
Else
{If (operation. Equals ("-"))
Display (count2.minus ()));
Else
{If (operation. Equals ("*"))
Display (count2.multiply ()));
Else
If (operation. Equals ("/"))
{If (data2 = 0)
{Resultarea. settext ("the divisor cannot be 0 ");
Operation = "";
Pnum = "";
Return;
}
Else
Display (count2.divide ()));
}
}
}
Operation = "";/* indicates the current character */
Pnum = resultarea. gettext ();/* The number of operations is the current number of texts */
}

Public void display (double result)/* display output method */
{Int A = (INT) result;
If (A = Result)
Resultarea. settext (string. valueof ());
Else
Resultarea. settext (string. valueof (result ));
If (resultarea. gettext (). Equals ("Nan "))
Resultarea. settext ("invalid input function ");
}

Private jpanel textpanel;/* textbox cotton panel */
Private jtextfield resultarea;/* text box */
Private jpanel controlpanel;/* control key Panel */
Private jpanel keypanel;/* number key and operator key Panel */
Private font font1 = new font ("dialog", 0, 10);/* Control Key font */
Private font font2 = new font ("dialog",);/* font of the number key and symbol key */
Private string pnum = "";/* first operand */
Private Boolean isnew = true;/* determines whether the number is new */
Private string operation = "";/* operator */
}


Class tester/* test class */
{
Public static void main (string [] ARGs)
{
New calculation ();
}
}

Related Article

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.