java - Validating a double to be between two values and GUI opens two JFrames -
so have been writing programme takes user input employee info , adds arraylist
display. programme working fine except 2 points. first trying validate user entered hourly rate between $6 , $160. here code that:
try{ double r = double.parsedouble(rate.gettext()); if (r >= 6 && r <= 150){ test.setrate(r); } } catch(numberformatexception e){ joptionpane.showmessagedialog(null, "sorry, hourly rate must between $6 , $150."); return; }
it validate between values, if not sets 0 rather display message user , homecoming out of method.
my sec problem little more complex, , honest lost. when run programme test or debug opens 2 windows. primary window blank controls exit_on_close
command while sec has coded it. have tried searching sec jframe
might have been initialized cannot find one. have larger amount of code , not sure might needed here code believe causing problem based off of behavior:
face = new jframe(); face.setsize(300,300); setdefaultcloseoperation(jframe.exit_on_close); grouplayout design = new grouplayout(face.getcontentpane()); face.setlayout(design); face.setvisible(true); design.setautocreategaps(true); design.setautocreatecontainergaps(true); design.setverticalgroup ( design.createsequentialgroup() .addgroup(design.createparallelgroup(grouplayout.alignment.leading) .addcomponent(fname) .addcomponent(first) .addcomponent(list)) .addgroup(design.createparallelgroup(grouplayout.alignment.leading) .addcomponent(lname) .addcomponent(last)) .addgroup(design.createparallelgroup(grouplayout.alignment.leading) .addcomponent(hwork) .addcomponent(hours)) .addgroup(design.createparallelgroup(grouplayout.alignment.leading) .addcomponent(hrate) .addcomponent(rate)) .addcomponent(admin) .addcomponent(market) .addcomponent(account) .addcomponent(prod) .addcomponent(sales) .addgroup(design.createparallelgroup(grouplayout.alignment.leading) .addcomponent(submit) .addcomponent(exit)) ); design.sethorizontalgroup ( design.createsequentialgroup() .addgroup(design.createparallelgroup(grouplayout.alignment.leading) .addcomponent(fname) .addcomponent(lname) .addcomponent(hwork) .addcomponent(hrate) .addcomponent(admin) .addcomponent(market) .addcomponent(account) .addcomponent(prod) .addcomponent(sales) .addcomponent(submit)) .addgroup(design.createparallelgroup(grouplayout.alignment.leading) .addcomponent(first) .addcomponent(last) .addcomponent(hours) .addcomponent(rate) .addcomponent(exit)) .addcomponent(list) ); }
other creating individual controls has gui design. insight welcome here! if forgot post happy so.
your first problem occurs because if r
not between 6 , 150 try
block doesn't throw numberformatexception
catch
isn't execute. need add together else
statement display message.
boolean showerror = false; try{ double r = double.parsedouble(rate.gettext()); if (r >= 6 && r <= 150){ test.setrate(r); }else{ showerror = true; } } catch(numberformatexception e){ showerror = true; } if(showerror){ joptionpane.showmessagedialog(null, "sorry, hourly rate must between $6 , $150."); }
since utilize setdefaultcloseoperation(jframe.exit_on_close);
straight suppose you're executing code in class extends jframe
means have 2 jframe
s 1 create when phone call class holds code , face
.
if have class extends jframe
, don't create other jframe
s, should remove:
face = new jframe();
java swing validation parsing
No comments:
Post a Comment