Java Simple Calculator class

Source: Internet
Author: User
Tags mul

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&NBSP;{PRIVATE&NBSP;JFRAME&NBSP;FRAME;PRIVATE&NBSP;JPANEL&NBSP;PANEL,PANELKEYS,PANELKEYS_UP, panelkeys_down;private jtextfield textcomputer;//Compute Area private jbutton buttonbk,buttonc;// Backspace and clear 0 key private jbutton button[];//number 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);p anel = 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 (); Textcomputer.settext (""); Textcomputer.seteditable (false); Textcomputer.setbackground (New color (255,255,255));//function key upper half part 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);p Anelkeys_up.add (BUTTONBK);p anelkeys_up.add (BUTTONC);//function key lower halfSub-panelkeys_down = new jpanel ();p anelkeys_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);p anelkeys_down.add (buttonadd);p anelkeys_down.add (buttonsub);p Anelkeys_ Down.add (Buttonmul);p anelkeys_down.add (buttondiv);p anelkeys_down.add (buttonequal);p anel.setlayout (new  BorderLayout ());p Anel.add (Textcomputer,borderlayout.north);p anel.add (Panelkeys_up,borderlayout.center); Panel.add (Panelkeys_down,borderlayout.south);} class computeractionlistener implements  actionlistener{@Overridepublic  void actionperformed (actionevent event)  {// TODO  Auto-generated method stubobject keybutton = event.getsource (); String text = textcomputer.gettext ();D Ecimalformat df = new decimalformat (" 0.########### ");//backspaceif (Keybutton == buttonbk && text.length ()  >  0) {Textcomputer.settext (text.substring (0,text.length ()-1));} C Key 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;} Symbolic 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 ();}}

Java Simple Calculator class

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.