Home>
package awtdemo;
import java.awt. *;
import java.awt.event. *;
import javax.swing. *;
import java.util.vector;
public class calculator {
  string str1="0";//Operand 1 must have an initial value of 0 for program safety
  string str2="0";//Operand 2
  string signal="+";//operator
  string result="";//result
  //status switch
  int k1=1;//switch 1 is used to select the input direction will be written to str2 or str2
  int k2=1;//switch 2 symbol key times k2>1 indicates that a multi-symbol operation such as 2 + 3-9 + 8 is performed
  int k3=1;//switch 3 str1 can be cleared to 0=yes when == 1! cannot be cleared to 0 when=1
  int k4=1;//switch 4 str2 same as above
  int k5=1;//switch 5 controls whether the decimal point can be entered=OK when == 1!=1 The decimal point entered is discarded
  jbutton store;//Register records whether the symbol key is continuously pressed
  @suppresswarnings ("rawtypes")
  vector vt=new vector (20, 10);
  jtextfield result_textfield=new jtextfield (result, 20);//20 columns
  jbutton clear_button=new jbutton ("clear");
  jbutton button0=new jbutton ("0");
  jbutton button1=new jbutton ("1");
  jbutton button2=new jbutton ("2");
  jbutton button3=new jbutton ("3");
  jbutton button4=new jbutton ("4");
  jbutton button5=new jbutton ("5");
  jbutton button6=new jbutton ("6");
  jbutton button7=new jbutton ("7");
  jbutton button8=new jbutton ("8");
  jbutton button9=new jbutton ("9");
  jbutton button_dian=new jbutton (".");
  jbutton button_jia=new jbutton ("+");
  jbutton button_jian=new jbutton ("-");
  jbutton button_cheng=new jbutton ("*");
  jbutton button_chu=new jbutton ("/");
  jbutton button_dy=new jbutton ("=");
  calculator () {
    button0.setmnemonic (keyevent.vk_0);//Equivalent key
    //other equivalent keys omitted,    result_textfield.sethorizontalalignment (jtextfield.right);//Right-align text box
    jpanel pan=new jpanel ();
    pan.setlayout (new gridlayout (4, 4, 5, 5));//four rows and four columns with a 5 pixel margin
    pan.add (button7);
    pan.add (button8);
    pan.add (button9);
    pan.add (button_chu);
    pan.add (button4);
    pan.add (button5);
    pan.add (button6);
    pan.add (button_cheng);
    pan.add (button1);
    pan.add (button2);
    pan.add (button3);
    pan.add (button_jian);
    pan.add (button0);
    pan.add (button_dian);
    pan.add (button_dy);
    pan.add (button_jia);
    pan.setborder (borderfactory.createemptyborder (5, 5, 5, 5));//The margin of the pan object
    jpanel pan2=new jpanel ();
    pan2.setlayout (new borderlayout ());
    pan2.add (result_textfield, borderlayout.west);
    pan2.add (clear_button, borderlayout.east);
    frame.setlocation (300, 200);//main window appears in position
    frame.setresizable (false);//cannot be resized
    frame.getcontentpane (). setlayout (new borderlayout ());
    frame.getcontentpane (). add (pan2, borderlayout.north);
    frame.getcontentpane (). add (pan, borderlayout.center);
    frame.pack ();
    frame.setvisible (true);
    //event handler
    //number key
    class listener implements actionlistener {
      @suppresswarnings ("unchecked")
      public void actionperformed (actionevent e) {
        string ss=((jbutton) e.getsource ()). gettext ();
        store=(jbutton) e.getsource ();
        vt.add (store);
        if (k1 == 1) {
          if (k3 == 1) {
            str1="";
            k5=1;//restore switch k5 state
          }
          str1=str1 + ss;
          k3=k3 + 1;
          result_textfield.settext (str1);//show
        } else if (k1 == 2) {
          if (k4 == 1) {
            str2="";
            k5=1;//restore switch k5 state
          }
          str2=str2 + ss;
          k4=k4 + 1;
          result_textfield.settext (str2);
        }
      }
    }
    //symbol
    class listener_signal implements actionlistener {
      @suppresswarnings ("unchecked")
      public void actionperformed (actionevent e) {
        string ss2=((jbutton) e.getsource ()). gettext ();
        store=(jbutton) e.getsource ();
        vt.add (store);
        if (k2 == 1) {
          k1=2;//When switch k1 is 1, write to the number 1 and write to the number 2
          k5=1;
          signal=ss2;
          k2=k2 + 1;//the number of times the symbol key was pressed
        } else {
          int a=vt.size ();
          jbutton c=(jbutton) vt.get (a-2);
          if (! (c.gettext (). equals ("+"))
              &&! (C.gettext (). Equals ("-"))
              &&! (C.gettext (). Equals ("*"))
              &&! (C.gettext (). Equals ("/")))
          {
            cal ();
            str1=result;
            k1=2;//When switch k1 is 1, write to the number 1 and write to the number 2
            k5=1;
            k4=1;
            signal=ss2;
          }
          k2=k2 + 1;
        }
      }
    }
    //clear
    class listener_clear implements actionlistener {
      @suppresswarnings ("unchecked")
      public void actionperformed (actionevent e) {
        store=(jbutton) e.getsource ();
        vt.add (store);
        k5=1;
        k2=1;
        k1=1;
        k3=1;
        k4=1;
        str1="0";
        str2="0";
        signal="";
        result="";
        result_textfield.settext (result);
        vt.clear ();
      }
    }
    //equal to
    class listener_dy implements actionlistener {
      @suppresswarnings ("unchecked")
      public void actionperformed (actionevent e) {
        store=(jbutton) e.getsource ();
        vt.add (store);
        cal ();
        k1=1;//restore switch k1 state
        //str1=result;
        k2=1;
        k3=1;//state of reset switch k3
        k4=1;//restore switch k4 state
        str1=result;//prepare for 7 + 5=12 + 5=17
      }
    }
    //decimal point
    class listener_xiaos implements actionlistener {
      @suppresswarnings ("unchecked")
      public void actionperformed (actionevent e) {
        store=(jbutton) e.getsource ();
        vt.add (store);
        if (k5 == 1) {
          string ss2=((jbutton) e.getsource ()). gettext ();
          if (k1 == 1) {
            if (k3 == 1) {
              str1="";
              k5=1;//restore switch k5 state
            }
            str1=str1 + ss2;
            k3=k3 + 1;
            result_textfield.settext (str1);//show
          } else if (k1 == 2) {
            if (k4 == 1) {
              str2="";
              k5=1;//restore switch k5 state
            }
            str2=str2 + ss2;
            k4=k4 + 1;
            result_textfield.settext (str2);
          }
        }
        k5=k5 + 1;
      }
    }
    //register listener
    listener_dy jt_dy=new listener_dy ();
    listener jt=new listener ();//listen to the number keys
    listener_signal jt_signal=new listener_signal ();//Pro listener sign key
    listener_clear jt_c=new listener_clear ();//clear key
    listener_xiaos jt_xs=new listener_xiaos ();//decimal point key
    button7.addactionlistener (jt);
    button8.addactionlistener (jt);
    button9.addactionlistener (jt);
    button_chu.addactionlistener (jt_signal);
    button4.addactionlistener (jt);
    button5.addactionlistener (jt);
    button6.addactionlistener (jt);
    button_cheng.addactionlistener (jt_signal);
    button1.addactionlistener (jt);
    button2.addactionlistener (jt);
    button3.addactionlistener (jt);
    button_jian.addactionlistener (jt_signal);
    button0.addactionlistener (jt);
    button_dian.addactionlistener (jt_xs);
    button_dy.addactionlistener (jt_dy);
    button_jia.addactionlistener (jt_signal);
    clear_button.addactionlistener (jt_c);
    //close the event handler
    frame.addwindowlistener (new windowadapter () {
      public void windowclosing (windowevent e) {
        system.exit (0);
      }
    });
  }
  //calculate
  public void cal () {
    double a2;//operand 1
    double b2;//operand 2
    string c=signal;//operator
    double result2=0;//result
    if (c.equals ("")) {
      result_textfield.settext ("Please enter an operator");
    } else {
      if (str1.equals (".")) //The string "." will be converted into double data, so an error will occur.
        str1="0.0";
      if (str2.equals ("."))
        str2="0.0";
      a2=double.valueof (str1) .doublevalue ();
      b2=double.valueof (str2) .doublevalue ();
      if (c.equals ("+")) {
        result2=a2 + b2;
      }
      if (c.equals ("-")) {
        result2=a2-b2;
      }
      if (c.equals ("*")) {
        result2=a2 * b2;
      }
      if (c.equals ("/")) {
        if (b2 == 0) {
          result2=0;
        } else {
          result2=a2/b2;
        }
      }
      result=((new double (result2)). tostring ());
      result_textfield.settext (result);
    }
  }
  @suppresswarnings ("unused")
  public static void main (string [] args) {
    //interface style,Can be removed
  try {
          uimanager.setlookandfeel ("com.sun.java.swing.plaf.windows.windowslookandfeel");
    } catch (exception e) {
      e.printstacktrace ();
    }
    calculator cal=new calculator ();
  }
}

running result:

PS:Here are a few more calculation tools for everyone to refer to:

  • Previous Example of generating help documentation for Java documentation comments
  • Next NodeJs form-data format transfer file method