java - Try and Catch for simple calculator input -


this program written in java serves calculator. can tell me how implement try/catch block if user tries input result text can handle incorrect workflow?

here code:

    package simplecal;       import java.awt.*;      import java.awt.event.*;      import javax.swing.*;   public class simplecal extends jframe {  jtextfield jftinput1, jftinput2, jftresult; jbutton jbtminus, jbtadd,jbtdivid, jbttimes; final int jtext_size = 5; simplecal(){     setlayout(new flowlayout());     jftinput1 = new jtextfield(jtext_size);     jftinput2 = new jtextfield(jtext_size);     jftresult = new jtextfield(jtext_size);     add(new jlabel("input 1: "));     add(jftinput1);     add(new jlabel("input 2: "));     add(jftinput2);     add(new jlabel("result "));     add(jftresult);     jpanel p1 = new jpanel();      jbtminus = new jbutton("subtract");     jbtadd = new jbutton("add");     jbttimes = new jbutton("multiple");     jbtdivid = new jbutton("divided");      p1.add(jbtminus);     jbtminus.addactionlistener(new buttonlistener1());     p1.add(jbtadd);     jbtadd.addactionlistener(new buttonlistener2());     p1.add(jbttimes);     jbttimes.addactionlistener(new buttonlistener3());     p1.add(jbtdivid);     jbtdivid.addactionlistener(new buttonlistener4());     add(p1, borderlayout.south);  }  class buttonlistener1 implements actionlistener{     public void actionperformed(actionevent e){         double input1 = double.parsedouble(jftinput1.gettext());         double input2 = double.parsedouble(jftinput2.gettext());          double answer = input1 - input2;          jftresult.settext(double.tostring(answer));     } }  class buttonlistener2 implements actionlistener{     public void actionperformed(actionevent e){        double input1 = double.parsedouble(jftinput1.gettext());         double input2 = double.parsedouble(jftinput2.gettext());          double answer = input1 + input2;          jftresult.settext(double.tostring(answer));     } }  class buttonlistener3 implements actionlistener{     public void actionperformed(actionevent e){        double input1 = double.parsedouble(jftinput1.gettext());         double input2 = double.parsedouble(jftinput2.gettext());          double answer = input1 * input2;          jftresult.settext(double.tostring(answer));     } }  class buttonlistener4 implements actionlistener{     public void actionperformed(actionevent e){        double input1 = double.parsedouble(jftinput1.gettext());         double input2 = double.parsedouble(jftinput2.gettext());          double answer = input1 / input2;          jftresult.settext(double.tostring(answer));     } }  public static void main(string[] args) {     simplecal frame = new simplecal();     frame.settitle("simple cal");     frame.pack();     frame.setlocationrelativeto(null);     frame.setdefaultcloseoperation(jframe.exit_on_close);     frame.setvisible(true);           frame.setresizable(false);     frame.setsize(450,100); } 

}

you don't need exception handler this. set field not editable:

jftresult.seteditable(false); 

see http://docs.oracle.com/javase/7/docs/api/javax/swing/text/jtextcomponent.html#seteditable(boolean).


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -