swing - Java: TextField Doesn't Convert to Integer Variable i -
i doing work text field make program in user type in text field converts int problem when user type doesn't convert int i.the condition not work correctly why happening.
public class extends jframe{ private jtextfield entervar; private jbutton button; public a(){ getcontentpane().setlayout(null); entervar = new jtextfield(); entervar.settooltiptext("variable must 'i'"); entervar.setbounds(37,26,89,28); getcontentpane().add(entervar); button = new jbutton("click"); button.addactionlistener(new actionlistener() { public void actionperformed(actionevent arg0) { string p = entervar.gettext(); try { int i; if ( p.equals("i") ) { = integer.parseint(p); } } catch (exception e) { joptionpane.showmessagedialog (null, "enter variable must i"); } } }); button.setbounds(108,121,89,28); getcontentpane().add(button); setsize(400,400); setdefaultcloseoperation(jframe.exit_on_close); setlocationrelativeto(null); setvisible(true); } }
main:
public class main { public static void main(string[] args) { obj = new a(); } }
p.equals("i")
means user enters "i" in text field. can't converted number "i" not number.
read jformattedtextfield. can set numbermodel int , integer directly.
Comments
Post a Comment