Home>
package chap;
import java.awt.borderlayout;
import java.awt.color;
import java.awt.flowlayout;
import java.awt.gridlayout;
import java.awt.event.actionevent;
import java.awt.event.actionlistener;
import java.text.decimalformat;
import javax.swing.jbutton;
import javax.swing.jframe;
import javax.swing.jpanel;
import javax.swing.jtextfield;
public class calculator {
  private jframe frame;
  private jpanel panel, panelkeys, panelkeys_up, panelkeys_down;
  private jtextfield textcomputer;//Calculation area
  private jbutton buttonbk, buttonc;//backspace and clear keys
  private jbutton button [];//Numeric key group
  private jbutton buttondot, buttonaddsub, buttonadd, buttonsub, buttonmul, buttondiv, button1, button2, button3, buttonequal;
  private double result;//calculation result
  private final short add=1;
  private final short sub=2;
  private final short mul=3;
  private final short div=4;
  private short operator=-1;//operator
  public calculator () {
    frame=new jframe ("Computer");
    frame.setsize (400, 250);
    panel=new jpanel ();//Global panel
    panel.setvisible (true);
    frame.setvisible (true);
    frame.getcontentpane (). add (panel);
    actionlistener listener=new computeractionlistener ();//Key listener
    //Calculation area
    textcomputer=new jtextfield (15);
    textcomputer.settext ("");
    textcomputer.seteditable (false);
    textcomputer.setbackground (new color (255,255,255));
    //The upper part of the function key
    panelkeys_up=new jpanel ();
    panelkeys_up.setlayout (new flowlayout (flowlayout.right));
    buttonbk=new jbutton ("backspace");
    buttonbk.setforeground (new color (255,0,0));
    buttonc=new jbutton ("c");
    buttonc.setforeground (new color (255,0,0));
    buttonbk.addactionlistener (listener);
    buttonc.addactionlistener (listener);
    panelkeys_up.add (buttonbk);
    panelkeys_up.add (buttonc);
    //The lower part of the function key
    panelkeys_down=new jpanel ();
    panelkeys_down.setlayout (new gridlayout (4,5));
    button=new jbutton [10];
    for (int i=0;i<button.length;i ++) {
      button [i]=new jbutton (integer.tostring (i));
      button [i] .setforeground (new color (255,0,0));
    }
    buttonaddsub=new jbutton ("+/-");
    buttonaddsub.setforeground (new color (255,0,0));
    buttonadd=new jbutton ("+");
    buttonadd.setforeground (new color (255,0,0));
    buttonsub=new jbutton ("-");
    buttonsub.setforeground (new color (255,0,0));
    buttonmul=new jbutton ("*");
    buttonmul.setforeground (new color (255,0,0));
    buttondiv=new jbutton ("/");
    buttondiv.setforeground (new color (255,0,0));
    button1=new jbutton ();
    button2=new jbutton ();
    button3=new jbutton ();
    button1.setforeground (new color (255,0,0));
    button2.setforeground (new color (255,0,0));
    button3.setforeground (new color (255,0,0));
    buttonequal=new jbutton ("=");
    buttonequal.setforeground (new color (255,0,0));
    buttonaddsub.addactionlistener (listener);
    buttonadd.addactionlistener (listener);
    buttonsub.addactionlistener (listener);
    buttonmul.addactionlistener (listener);
    buttondiv.addactionlistener (listener);
    buttonequal.addactionlistener (listener);
    for (int i=0;i<= 9;i ++) {
      button [i] .addactionlistener (listener);
    }
    for (int i=0;i<= 9;i ++) {
      panelkeys_down.add (button [i]);
    }
    panelkeys_down.add (buttonaddsub);
    panelkeys_down.add (buttonadd);
    panelkeys_down.add (buttonsub);
    panelkeys_down.add (buttonmul);
    panelkeys_down.add (buttondiv);
    panelkeys_down.add (buttonequal);
    panel.setlayout (new borderlayout ());
    panel.add (textcomputer, borderlayout.north);
    panel.add (panelkeys_up, borderlayout.center);
    panel.add (panelkeys_down, borderlayout.south);
  }
  class computeractionlistener implements actionlistener {
    @override
    public void actionperformed (actionevent event) {
      //todo auto-generated method stub
      object keybutton=event.getsource ();
      string text=textcomputer.gettext ();
      decimalformat df=new decimalformat ("0. #############");
      //backspace
      if (keybutton == buttonbk&&text.length ()>0) {
        textcomputer.settext (text.substring (0, text.length ()-1));
      }
      //c
      if (keybutton == buttonc) {
        result=0;
        textcomputer.settext ("");
      }
      //number key
      for (int i=0;i<10;i ++) {
        if (keybutton == button [i]) {
          textcomputer.settext (text + i);
        }
      }
      if (keybutton == buttonadd) {
        operator=1;
      }
      if (keybutton == buttonsub) {
        operator=2;
      }
      if (keybutton == buttonmul) {
        operator=3;
      }
      if (keybutton == buttondiv) {
        operator=4;
      }
      //Symbol key
      if (keybutton == buttonadd || keybutton == buttonsub || keybutton == buttonmul || keybutton == buttondiv || keybutton == buttonequal) {
        switch (operator) {
        case add:
          result +=double.parsedouble (text);
          break;
        case sub:
          result-= double.parsedouble (text);
          break;
        case mul:
          result *=double.parsedouble (text);
          break;
        case div:
          result /=double.parsedouble(text);
          break;
        default:
          result=double.parsedouble (text);
        }
        textcomputer.settext ("");
      }
      if (keybutton == buttonequal) {
        textcomputer.settext (string.valueof (result));
      }
    }
  }
  public static void main (string args []) {
    new calculator ();
  }
}
  • Previous Detailed Application of Vector in Java Programming
  • Next jQuery plugin Timelinr for timeline effects