java - jOption message not closing -


hello apologises if stupid question i'm struggling work out why joptionpane message not closing. i'm wanting use nested control structure countup amount of calculations performed user.

i figure it's how i'm either counting or how i'm testing see if condition has been met dispose of calculator frame (i'm going new thing happen i'm wanting whole thing close)

package ac.uk.valley.forth;  import java.awt.borderlayout; import java.awt.eventqueue;  import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.border.emptyborder;    import javax.swing.jbutton; import java.awt.event.actionlistener; import java.awt.event.actionevent; import javax.swing.jtextfield; import javax.swing.joptionpane;  public class calculator extends jframe {  protected static final string operatorbutton = null; private jpanel contentpane; private jtextfield textfield; private double total = 0.0; private double actualtotal = 0.0; private char operator; public int counter = 0;  /**  * launch application.  */ public static void main(string[] args) {     eventqueue.invokelater(new runnable() {         public void run() {             try {                 calculator frame = new calculator();                 frame.setvisible(true);             } catch (exception e) {                 e.printstacktrace();             }         }     }); }  /**  * create frame.  */ public calculator() {      settitle("calculator");     setresizable(false);     setdefaultcloseoperation(jframe.exit_on_close);     setbounds(100, 100, 291, 330);     contentpane = new jpanel();     contentpane.setborder(new emptyborder(5, 5, 5, 5));     contentpane.setlayout(new borderlayout(0, 0));     setcontentpane(contentpane);      jpanel panel = new jpanel();     contentpane.add(panel, borderlayout.center);     panel.setlayout(null);      textfield = new jtextfield();     textfield.setbounds(0, 37, 275, 85);     panel.add(textfield);     textfield.setcolumns(10);      jbutton button_10 = new jbutton("7");     button_10.setbounds(5, 157, 44, 23);     button_10.addactionlistener(new actionlistener() {         public void actionperformed(actionevent e) {             textfield.settext(textfield.gettext().concat("7"));         }     });     panel.add(button_10);      jbutton button_11 = new jbutton("8");     button_11.setbounds(55, 157, 44, 23);     button_11.addactionlistener(new actionlistener() {         public void actionperformed(actionevent e) {             textfield.settext(textfield.gettext().concat("8"));         }     });     panel.add(button_11);      jbutton button_12 = new jbutton("9");     button_12.setbounds(109, 157, 44, 23);     button_12.addactionlistener(new actionlistener() {         public void actionperformed(actionevent e) {             textfield.settext(textfield.gettext().concat("9"));         }     });     panel.add(button_12);      jbutton btnoff = new jbutton("off");     btnoff.setbounds(158, 157, 61, 23);     btnoff.addactionlistener(new actionlistener() {         public void actionperformed(actionevent e) {              // setvisible(false);             // dispose();          }     });     panel.add(btnoff);      jbutton button_4 = new jbutton("4");     button_4.setbounds(5, 185, 44, 23);     button_4.addactionlistener(new actionlistener() {         public void actionperformed(actionevent e) {             textfield.settext(textfield.gettext().concat("4"));         }     });     panel.add(button_4);      jbutton button_5 = new jbutton("5");     button_5.setbounds(55, 185, 44, 23);     button_5.addactionlistener(new actionlistener() {         public void actionperformed(actionevent e) {             textfield.settext(textfield.gettext().concat("5"));         }     });     panel.add(button_5);      jbutton button_6 = new jbutton("6");     button_6.setbounds(109, 185, 44, 23);     button_6.addactionlistener(new actionlistener() {         public void actionperformed(actionevent e) {             textfield.settext(textfield.gettext().concat("6"));         }     });     panel.add(button_6);      jbutton button = new jbutton("1");     button.setbounds(5, 213, 44, 23);     button.addactionlistener(new actionlistener() {         public void actionperformed(actionevent e) {             textfield.settext(textfield.gettext().concat("1"));         }     });     panel.add(button);      jbutton button_1 = new jbutton("2");     button_1.setbounds(55, 213, 44, 23);     button_1.addactionlistener(new actionlistener() {         public void actionperformed(actionevent e) {             textfield.settext(textfield.gettext().concat("2"));         }     });     panel.add(button_1);      jbutton button_2 = new jbutton("3");     button_2.setbounds(109, 213, 44, 23);     button_2.addactionlistener(new actionlistener() {         public void actionperformed(actionevent e) {             textfield.settext(textfield.gettext().concat("3"));         }     });     panel.add(button_2);      jbutton button_13 = new jbutton("0");     button_13.setbounds(5, 240, 44, 23);     button_13.addactionlistener(new actionlistener() {         public void actionperformed(actionevent e) {              textfield.settext(textfield.gettext().concat("0"));          }     });     panel.add(button_13);      jbutton button_14 = new jbutton(".");     button_14.setbounds(56, 241, 44, 23);     button_14.addactionlistener(new actionlistener() {         public void actionperformed(actionevent e) {             textfield.settext(textfield.gettext().concat("."));          }     });     panel.add(button_14);      jbutton btnce_1 = new jbutton("ce");     btnce_1.setbounds(109, 240, 53, 23);     btnce_1.addactionlistener(new actionlistener() {         public void actionperformed(actionevent e) {              textfield.settext("");         }     });     panel.add(btnce_1);      jbutton equalsbutton = new jbutton("=");     equalsbutton.setbounds(166, 212, 53, 51);     equalsbutton.addactionlistener(new actionlistener() {         public void actionperformed(actionevent e) {              calculate();          }     });     panel.add(equalsbutton);      final jbutton subtractionbutton = new jbutton("-");     subtractionbutton.setbounds(221, 213, 44, 23);     subtractionbutton.addactionlistener(new actionlistener() {         public void actionperformed(actionevent e) {             // textfield.settext(textfield.gettext().concat("-"));              string operatortext = subtractionbutton.gettext();             getoperator(operatortext);          }     });     panel.add(subtractionbutton);      final jbutton modulobutton = new jbutton("%");     modulobutton.setbounds(221, 157, 44, 23);     modulobutton.addactionlistener(new actionlistener() {         public void actionperformed(actionevent e) {             // textfield.settext(textfield.gettext().concat("%"));             string operatortext = modulobutton.gettext();             getoperator(operatortext);          }     });     panel.add(modulobutton);      final jbutton multiplicationbutton = new jbutton("*");     multiplicationbutton.setbounds(167, 185, 44, 23);     multiplicationbutton.addactionlistener(new actionlistener() {         public void actionperformed(actionevent e) {             // textfield.settext(textfield.gettext().concat("*"));              string operatortext = multiplicationbutton.gettext();             getoperator(operatortext);          }     });      panel.add(multiplicationbutton);      final jbutton dividebutton = new jbutton("/");     dividebutton.setbounds(221, 185, 44, 23);     dividebutton.addactionlistener(new actionlistener() {         public void actionperformed(actionevent e) {             // textfield.settext(textfield.gettext().concat("/"));              string operatortext = dividebutton.gettext();             getoperator(operatortext);         }     });     panel.add(dividebutton);      final jbutton additionbutton = new jbutton("+");     additionbutton.setbounds(221, 240, 44, 23);     additionbutton.addactionlistener(new actionlistener() {         public void actionperformed(actionevent e) {              string operatortext = additionbutton.gettext();             getoperator(operatortext);          }     });     panel.add(additionbutton); }  private void getoperator(string operatorbutton) {      operator = operatorbutton.charat(0);     total = total + double.parsedouble(textfield.gettext());     textfield.settext("");  }  public void calculate() {      switch (operator) {     case '+':          actualtotal = total + double.parsedouble(textfield.gettext());          break;      case '-':         actualtotal = total - double.parsedouble(textfield.gettext());          break;     case '/':         actualtotal = total / double.parsedouble(textfield.gettext());          break;     case '*':         actualtotal = total * double.parsedouble(textfield.gettext());          break;     case '%':         actualtotal = total % double.parsedouble(textfield.gettext());          break;      }      textfield.settext(double.tostring(actualtotal));     total = 0;     counter++;     other(counter);  }  public void other(int counter) {      boolean ongoing = true;     string infomessage = integer.tostring(counter);           {          joptionpane.showmessagedialog(null, infomessage, "infobox: " + "calculations performed",                 joptionpane.information_message);         joptionpane.getrootframe().dispose();          if (counter > 2)          {              ongoing = false;             setvisible(false);          }     } while (ongoing == true);  } 

}

you've got while (true) block stomping on swing event thread , preventing joptionpane closing:

while (ongoing == true); 

solution: rid of it; don't need it, since event-driven program. instead put code in events (your actionlisteners).

some asides:

  • while null layouts , setbounds() might seem swing newbies easiest , best way create complex gui's, more swing gui's create more serious difficulties run when using them. won't resize components when gui resizes, royal witch enhance or maintain, fail when placed in scrollpanes, gawd-awful when viewed on platforms or screen resolutions different original one.
  • use arrays , arraylists loops shorten of code making easier coding , debugging.

Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo