Java calculator nearly complete -


i need making calculator buttons able enter multi digit numbers. thanks. complete , school project. have been unable find answer problem. if knows add allow multidigit numbers text field using buttons appreciated.

 //these imports used in code   import java.awt.button; import java.awt.dimension; import java.awt.eventqueue; import java.awt.font; import java.awt.gridlayout; import java.awt.borderlayout; import java.awt.insets; import java.awt.textfield; import java.awt.event.actionlistener; import java.awt.event.actionevent; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jtextfield; import javax.swing.jbutton; import java.awt.container;  public class calculatorfinal implements actionlistener{      jframe guiframe;     jpanel buttonpanel;     jtextfield tf1;      int calcoperation = 0;     int currentcalc;     int p = 0;       public static void main(string[] args) {           //use event dispatch thread swing components          eventqueue.invokelater(new runnable()          {              @override              public void run()              {                   new calculatorfinal();                       }          });      }      public calculatorfinal()     {         guiframe = new jframe();          //this exits program when frame closes         guiframe.setdefaultcloseoperation(jframe.exit_on_close);         guiframe.settitle("calculator");         guiframe.setsize(300,350);          //this centre jframe in middle of screen         guiframe.setlocationrelativeto(null);         //this sets tf1 (text field 1), allows editable aligns text     right         tf1 = new jtextfield();         tf1.sethorizontalalignment(jtextfield.right);         tf1.seteditable(true);          guiframe.add(tf1, borderlayout.north);          buttonpanel = new jpanel();          //grid has 5 rows , 3 columns         buttonpanel.setlayout(new gridlayout(6,3));            guiframe.add(buttonpanel, borderlayout.center);          //adding number buttons          (int i=1;i<10;i++)           {        addbutton(buttonpanel, string.valueof(tf1.gettext()+i));         }           {             addbutton(buttonpanel, string.valueof(tf1.gettext()+p));           }          jbutton buttondecimal = new jbutton(".");         buttondecimal.addactionlistener(this);         buttonpanel.add(buttondecimal);          jbutton buttonplus = new jbutton("+");         buttonplus.setfont(new font("verdana", font.bold, 20));         buttonplus.setactioncommand("+");          operatoraction subaction = new operatoraction(1);         buttonplus.addactionlistener(subaction);          jbutton buttonminus = new jbutton("-");         buttonminus.setfont(new font("verdana", font.bold, 20));         buttonminus.setactioncommand("-");          operatoraction addaction = new operatoraction(2);         buttonminus.addactionlistener(addaction);          jbutton buttontimes = new jbutton("*");         buttontimes.setfont(new font("verdana", font.bold, 20));         buttontimes.setactioncommand("*");          operatoraction multiplyaction = new operatoraction(3);         buttontimes.addactionlistener(multiplyaction);          jbutton buttondivide = new jbutton("/");         buttondivide.setfont(new font("verdana", font.bold, 20));         buttondivide.setactioncommand("/");          operatoraction actiondivide = new operatoraction(4);         buttondivide.addactionlistener(actiondivide);          jbutton buttonclear = new jbutton("clear");         buttonclear.addactionlistener(new actionlistener(){             public void actionperformed(actionevent e){                 tf1.settext("");                 //textfield.settext(null);             }         });          jbutton buttonequals = new jbutton("=");         buttonequals.setfont(new font("verdana", font.bold, 20));         buttonequals.setactioncommand("=");         buttonequals.addactionlistener(new actionlistener()          {             @override             public void actionperformed(actionevent event)             {                 if (!tf1.gettext().isempty())                 {                     double number = double.parsedouble(tf1.gettext());                      if (calcoperation == 1)                     {                         double calculate = currentcalc  + number;                         tf1.settext(double.tostring(calculate));                     }                     else if (calcoperation == 2)                     {                         double calculate = currentcalc  - number;                         tf1.settext(double.tostring(calculate));                     }                     else if (calcoperation == 3)                      {                 double calculate = currentcalc  * number;                 tf1.settext(double.tostring(calculate));                 }     else if (calcoperation == 4)                      {                 double calculate = currentcalc  / number;                 tf1.settext(double.tostring(calculate));                 } }}});          buttonpanel.add(buttonplus);         buttonpanel.add(buttonminus);         buttonpanel.add(buttonequals);         buttonpanel.add(buttontimes);         buttonpanel.add(buttondivide);         buttonpanel.add(buttonclear);          guiframe.setvisible(true);       }      //all buttons following same pattern     //so create them in 1 place.     private void addbutton(container parent, string name)     {         jbutton = new jbutton(name);         but.setactioncommand(name);         but.addactionlistener(this);         parent.add(but);     }      //as buttons doing same thing it's     //easier make class implement actionlistener     //interface , control button clicks 1 place     @override     public void actionperformed(actionevent event)     {         //get action command text button         string action = event.getactioncommand();          //set text using action command text         tf1.settext(action);            }      private class operatoraction implements actionlistener     {         private int operator;          public operatoraction(int operation)         {            operator = operation;         }          public void actionperformed(actionevent event)         {             currentcalc = integer.parseint(tf1.gettext());              calcoperation = operator;         }     } } 

you setting value action rather previous value , action

 tf1.settext(action) 

should

 tf1.settext(tf1.gettext() + action) 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -