The Code repetition rate is too high. It should be optimized and there is no time.
The setactioncommand () method can greatly reduce the amount of code.
Code:
- Import java. AWT .*;
- Import java. AWT. event .*;
- Import javax. Swing .*;
- Public class test1 {
- Public static void main (string [] ARGs ){
- Calculator c = new calculator ();
- C. setvisible (true );
- }
- }
- Class calculator extends jframe implements actionlistener {
- Container P;
- Jsplitpane JSP;
- Jtextfield resultfiled = new jtextfield ("0 ");
- Jpanel resultpane = new jpanel ();
- Jpanel btnpane = new jpanel ();
- Static float OP1 = 0;
- Static int Len = 0;
- Static float OP2 = 0;
- Static string output = "";
- Static string OPP = "";
- Static Boolean flag1 = true;
- Static Boolean flag2 = true;
- String [] btns = {"1", "2", "3", "+ ",
- "4", "5", "6 ","-",
- "7", "8", "9 ","*",
- "0", "= ",".","/"};
- Jbutton [] BTN = new jbutton [16];
- Public calculator (){
- P = getcontentpane ();
- Setbounds (100,100,300,300 );
- Settitle ("Calculator ");
- Setresizable (false );
- Seticonimage (New imageicon ("16.png"). getimage (); // set the icon displayed in the form
- Setdefaclocloseoperation (jframe. exit_on_close );
- Setcursor (cursor. getpredefinedcursor (cursor. hand_cursor); // set the Mouse shape
- This. setbackground (new color (255, 0,255 ));
- Jbutton cz = new jbutton ("reset ");
- CZ. setbounds (0, 0, 80, 20 );
- CZ. setbackground (new color (65,105,225 ));
- CZ. setforeground (new color (255, 0, 0 ));
- CZ. addactionlistener (this );
- Resultfiled. seteditable (false );
- Resultfiled. sethorizontalalignment (jtextfield. Right); // set the display from right to left
- Resultfiled. setcolumns (12 );
- Resultfiled. setfont (new font ("", Font. Plain, 14 ));
- Resultfiled. setbackground (new color (0,255,127 ));
- Resultfiled. setforeground (new color (255, 0, 0 ));
- Jbutton QX = new jbutton ("cancel ");
- QX. addactionlistener (this );
- QX. setbackground (new color (65,105,225 ));
- QX. setforeground (new color (255, 0, 0 ));
- QX. setsize (80, 20 );
- Resultpane. setlayout (New flowlayout ());
- Resultpane. Add (CZ );
- Resultpane. Add (resultfiled );
- Resultpane. Add (QX );
- Btnpane. setlayout (New gridlayout (4, 5, 7, 7 ));
- For (INT I = 0; I <16; I ++ ){
- BTN [I] = new jbutton (btns [I]);
- BTN [I]. setborderpainted (true );
- BTN [I]. setcontentareafilled (true );
- BTN [I]. setfont (new font ("", Font. Bold, 25 ));
- BTN [I]. setbackground (new color (255,255, 0 ));
- BTN [I]. setforeground (new color (255, 0, 0 ));
- BTN [I]. addactionlistener (this );
- Btnpane. Add (BTN [I]);
- }
- JSP = new jsplitpane (jsplitpane. vertical_split, resultpane, btnpane );
- JSP. setdividersize (3 );
- JSP. setdividerlocation (35 );
- P. Add (JSP, borderlayout. center );
- }
- Public void actionreceivmed (actionevent e ){
- If (flag1 &&
- (E. getactioncommand () = "1" |
- E. getactioncommand () = "2" |
- E. getactioncommand () = "3" |
- E. getactioncommand () = "4" |
- E. getactioncommand () = "5" |
- E. getactioncommand () = "6" |
- E. getactioncommand () = "7" |
- E. getactioncommand () = "8" |
- E. getactioncommand () = "9" |
- E. getactioncommand () = "0" |
- E. getactioncommand () = ".")){
- Output + = E. getactioncommand ();
- Resultfiled. settext (output );
- }
- If (E. getactioncommand () = "= "){
- If (flag1 ){
- OP2 = float. parsefloat (resultfiled. gettext (). substring (LEN + 1 ));
- Output + = E. getactioncommand ();
- If (OPP = "+ "){
- Output + = (OP1 + OP2) + "";
- } If (OPP = "-"){
- Output + = (OP1 + OP2) + "";
- } If (OPP = "*"){
- Output + = (OP1 * OP2) + "";
- } If (OPP = "/"){
- If (op2-0.0 = 0 ){
- Output + = "error ";
- } Else {
- Output + = (OP1/OP2) + "";
- }
- }
- Resultfiled. settext (output );
- Flag1 = false;
- }
- }
- If (E. getactioncommand () = "reset "){
- OP1 = 0;
- OP2 = 0;
- OPP = "";
- Output = "";
- Flag1 = true;
- Flag2 = true;
- Resultfiled. settext ("0 ");
- }
- If (E. getactioncommand () = "+" |
- E. getactioncommand () = "-" |
- E. getactioncommand () = "*" |
- E. getactioncommand () = "/"){
- If (flag2 ){
- OP1 = float. parsefloat (resultfiled. gettext ());
- Len = resultfiled. gettext (). Length ();
- OPP = E. getactioncommand ();
- Output + = E. getactioncommand ();
- Resultfiled. settext (output );
- Flag2 = false;
- }
- }
- }
- }