A simple calculator using Java

Source: Internet
Author: User
Tags mul

There are two classes in total. It only completes the +,-, X, and merge operations.

The GUI is only usedAWTIt is very easy to understand.
Calculator. Java

Public class calculator {
Private string result = "0 ";
Private int op = 0, add = 1, sub = 2, mul = 3, DIV = 4;

Private double stringtodouble (string X ){
Double Y = double. parsedouble (X );
Return y;
}
Private void operate (string X ){
Double X1 = stringtodouble (X );
Double Y = stringtodouble (result );
Switch (OP ){
Case 0:
Result = X;
Break;
Case 1:
Result = string. valueof (Y + X1 );
Break;
Case 2:
Result = string. valueof (y-x1 );
Break;
Case 3:
Result = string. valueof (y * X1 );
Break;
Case 4:
If (x1! = 0 ){
Result = string. valueof (y/X1 );
} Else {
Result = "the divisor can" t be zero! ";
}

Java heaven

Break;
}
}

Public String opadd (string X ){
Operate (X );
OP = add;
Return result;
}
Public String opsubtract (string X ){
Operate (X );
OP = sub;
Return result;
}
Public String opmultiply (string X ){
Operate (X );
OP = Mul;
Return result;
}
Public String opdivide (string X ){
Operate (X );
OP = div;
Return result;
}
Public String opequals (string X ){
Operate (X );
OP = 0;
Return result;
}
Public void opclean (){
OP = 0;
Result = "0 ";
}
}

-------------------------------------------------------------------

Second
Calculatorgui. Java

Import java. AWT .*;
Import java. AWT. event .*;
Import java. util. eventobject;

Public class calculatorgui {
Private frame F;
Private panel P1, P2;

Java heaven

Private button B0, B1, B2, B3, B4, B5, B6, B7, B8, B9;
Private button bpoint, BADD, bdec, bmul, bdiv, bcal;
Private textfield TF;
Private string S, OP;
Private calculator Cal = new calculator ();
Private Boolean IFOP;

Public calculatorgui (){
F = new frame ("Calculator ");
P1 = new Panel ();
P2 = new Panel ();

B0 = new button ("0 ");
B1 = new button ("1 ");
B2 = new button ("2 ");
B3 = new button ("3 ");
B4 = new button ("4 ");
B5 = new button ("5 ");
B6 = new button ("6 ");
B7 = new button ("7 ");
B8 = new button ("8 ");
B9 = new button ("9 ");
Bpoint = new button (".");
BADD = new button ("+ ");
Bdec = new button ("-");
Bmul = new button ("*");
Bdiv = new button ("/");

Java heaven

Bcal = new button ("= ");

TF = new textfield (25 );
TF. seteditable (false );

}

Public void launchframe (){
F. setsize (220,160 );
F. setresizable (false );
F. addwindowlistener (New mywindowlistener ());
P1.setlayout (New flowlayout (flowlayout. Center ));
P1.add (TF );
F. Add (P1, borderlayout. North );
P2.setlayout (New gridlayout (4, 4 ));

B0.addactionlistener (New setlabeltext_actionlistener ());
B1.addactionlistener (New setlabeltext_actionlistener ());
B2.addactionlistener (New setlabeltext_actionlistener ());
B3.addactionlistener (New setlabeltext_actionlistener ());
B4.addactionlistener (New setlabeltext_actionlistener ());
B5.addactionlistener (New setlabeltext_actionlistener ());
B6.addactionlistener (New setlabeltext_actionlistener ());
B7.addactionlistener (New setlabeltext_actionlistener ());
B8.addactionlistener (New setlabeltext_actionlistener ());
B9.addactionlistener (New setlabeltext_actionlistener ());
Bpoint. addactionlistener (New setlabeltext_actionlistener ());
BADD. addactionlistener (New setoperator_actionlistener ());
Bdec. addactionlistener (New setoperator_actionlistener ());
Bmul. addactionlistener (New setoperator_actionlistener ());
Bdiv. addactionlistener (New setoperator_actionlistener ());
Bcal. addactionlistener (New setoperator_actionlistener ());

P2.add (B7 );
P2.add (B8 );
P2.add (B9 );
P2.add (BADD );
P2.add (B4 );
P2.add (B5 );
P2.add (B6 );
P2.add (bdec );
P2.add (B1 );
P2.add (B2 );
P2.add (B3 );
P2.add (bmul );
P2.add (B0 );
P2.add (bpoint );
P2.add (bcal );
P2.add (bdiv );
F. Add (P2, borderlayout. South );
F. setvisible (true );
}

Public void settextfieldtext_temp (){
If (Tf. gettext (). Length () <15 & (Tf. gettext (). indexof (".") =-1 |! S. Equals ("."))){
TF. settext (Tf. gettext () + S );
} Else {
TF. settext (Tf. gettext () + S). substring (0, 15 ));
}
}
Public void settextfieldtext (){

If (IFOP ){
IFOP = false;
TF. settext ("");
Settextfieldtext_temp ();
} Else {
Settextfieldtext_temp ();
}
}

Public static void main (string [] ARGs ){
Calculatorgui calculator = new calculatorgui ();
Calculator. launchframe ();
}

Class mywindowlistener extends windowadapter {
Public void windowclosing (windowevent e ){
System. Exit (0 );
}
}

Class setlabeltext_actionlistener implements actionlistener {
Public void actionreceivmed (actionevent e ){
Button tempb = (button) E. getsource ();
S = tempb. getlabel ();
Settextfieldtext ();
}
}

Class setoperator_actionlistener implements actionlistener {
Public void actionreceivmed (actionevent e ){
Button tempb = (button) E. getsource ();
OP = tempb. getlabel ();
If (op. Equals ("+ ")){
TF. settext (Cal. opadd (Tf. gettext (); Java heaven
IFOP = true;
} Else if (op. Equals ("-")){
TF. settext (Cal. opsubtract (Tf. gettext ()));
IFOP = true;
} Else if (op. Equals ("*")){
TF. settext (Cal. opmultiply (Tf. gettext ()));
IFOP = true;
} Else if (op. Equals ("/")){
TF. settext (Cal. opdivide (Tf. gettext ()));
IFOP = true;
} Else if (op. Equals ("= ")){
TF. settext (Cal. opequals (Tf. gettext ()));
IFOP = true;
}
}
}
}

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.