Java swing simple calculator

Source: Internet
Author: User

 

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:
  1. Import java. AWT .*;
  2. Import java. AWT. event .*;
  3. Import javax. Swing .*;
  4. Public class test1 {
  5. Public static void main (string [] ARGs ){
  6. Calculator c = new calculator ();
  7. C. setvisible (true );
  8. }
  9. }
  10. Class calculator extends jframe implements actionlistener {
  11. Container P;
  12. Jsplitpane JSP;
  13. Jtextfield resultfiled = new jtextfield ("0 ");
  14. Jpanel resultpane = new jpanel ();
  15. Jpanel btnpane = new jpanel ();
  16. Static float OP1 = 0;
  17. Static int Len = 0;
  18. Static float OP2 = 0;
  19. Static string output = "";
  20. Static string OPP = "";
  21. Static Boolean flag1 = true;
  22. Static Boolean flag2 = true;
  23. String [] btns = {"1", "2", "3", "+ ",
  24. "4", "5", "6 ","-",
  25. "7", "8", "9 ","*",
  26. "0", "= ",".","/"};
  27. Jbutton [] BTN = new jbutton [16];
  28. Public calculator (){
  29. P = getcontentpane ();
  30. Setbounds (100,100,300,300 );
  31. Settitle ("Calculator ");
  32. Setresizable (false );
  33. Seticonimage (New imageicon ("16.png"). getimage (); // set the icon displayed in the form
  34. Setdefaclocloseoperation (jframe. exit_on_close );
  35. Setcursor (cursor. getpredefinedcursor (cursor. hand_cursor); // set the Mouse shape
  36. This. setbackground (new color (255, 0,255 ));
  37. Jbutton cz = new jbutton ("reset ");
  38. CZ. setbounds (0, 0, 80, 20 );
  39. CZ. setbackground (new color (65,105,225 ));
  40. CZ. setforeground (new color (255, 0, 0 ));
  41. CZ. addactionlistener (this );
  42. Resultfiled. seteditable (false );
  43. Resultfiled. sethorizontalalignment (jtextfield. Right); // set the display from right to left
  44. Resultfiled. setcolumns (12 );
  45. Resultfiled. setfont (new font ("", Font. Plain, 14 ));
  46. Resultfiled. setbackground (new color (0,255,127 ));
  47. Resultfiled. setforeground (new color (255, 0, 0 ));
  48. Jbutton QX = new jbutton ("cancel ");
  49. QX. addactionlistener (this );
  50. QX. setbackground (new color (65,105,225 ));
  51. QX. setforeground (new color (255, 0, 0 ));
  52. QX. setsize (80, 20 );
  53. Resultpane. setlayout (New flowlayout ());
  54. Resultpane. Add (CZ );
  55. Resultpane. Add (resultfiled );
  56. Resultpane. Add (QX );
  57. Btnpane. setlayout (New gridlayout (4, 5, 7, 7 ));
  58. For (INT I = 0; I <16; I ++ ){
  59. BTN [I] = new jbutton (btns [I]);
  60. BTN [I]. setborderpainted (true );
  61. BTN [I]. setcontentareafilled (true );
  62. BTN [I]. setfont (new font ("", Font. Bold, 25 ));
  63. BTN [I]. setbackground (new color (255,255, 0 ));
  64. BTN [I]. setforeground (new color (255, 0, 0 ));
  65. BTN [I]. addactionlistener (this );
  66. Btnpane. Add (BTN [I]);
  67. }
  68. JSP = new jsplitpane (jsplitpane. vertical_split, resultpane, btnpane );
  69. JSP. setdividersize (3 );
  70. JSP. setdividerlocation (35 );
  71. P. Add (JSP, borderlayout. center );
  72. }
  73. Public void actionreceivmed (actionevent e ){
  74. If (flag1 &&
  75. (E. getactioncommand () = "1" |
  76. E. getactioncommand () = "2" |
  77. E. getactioncommand () = "3" |
  78. E. getactioncommand () = "4" |
  79. E. getactioncommand () = "5" |
  80. E. getactioncommand () = "6" |
  81. E. getactioncommand () = "7" |
  82. E. getactioncommand () = "8" |
  83. E. getactioncommand () = "9" |
  84. E. getactioncommand () = "0" |
  85. E. getactioncommand () = ".")){
  86. Output + = E. getactioncommand ();
  87. Resultfiled. settext (output );
  88. }
  89. If (E. getactioncommand () = "= "){
  90. If (flag1 ){
  91. OP2 = float. parsefloat (resultfiled. gettext (). substring (LEN + 1 ));
  92. Output + = E. getactioncommand ();
  93. If (OPP = "+ "){
  94. Output + = (OP1 + OP2) + "";
  95. } If (OPP = "-"){
  96. Output + = (OP1 + OP2) + "";
  97. } If (OPP = "*"){
  98. Output + = (OP1 * OP2) + "";
  99. } If (OPP = "/"){
  100. If (op2-0.0 = 0 ){
  101. Output + = "error ";
  102. } Else {
  103. Output + = (OP1/OP2) + "";
  104. }
  105. }
  106. Resultfiled. settext (output );
  107. Flag1 = false;
  108. }
  109. }
  110. If (E. getactioncommand () = "reset "){
  111. OP1 = 0;
  112. OP2 = 0;
  113. OPP = "";
  114. Output = "";
  115. Flag1 = true;
  116. Flag2 = true;
  117. Resultfiled. settext ("0 ");
  118. }
  119. If (E. getactioncommand () = "+" |
  120. E. getactioncommand () = "-" |
  121. E. getactioncommand () = "*" |
  122. E. getactioncommand () = "/"){
  123. If (flag2 ){
  124. OP1 = float. parsefloat (resultfiled. gettext ());
  125. Len = resultfiled. gettext (). Length ();
  126. OPP = E. getactioncommand ();
  127. Output + = E. getactioncommand ();
  128. Resultfiled. settext (output );
  129. Flag2 = false;
  130. }
  131. }
  132. }
  133. }

 

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.