java - How to store the value in the JLabel without it resetting each time the JButton is activated? -
i'm creating program when enter value greater 18 adds 1 jlabel each time click button program resets 1 instead of adding additional one.
for example if enter value greater 18, jlabel should add additional 1 total two...
this code:
int age = integer.parseint(jtextfield1.gettext()); // gets value button click if(age >= 18){ // determines if greater 18 int totalone = 0; totalone = totalone + 1; string totalageone = integer.tostring(totalone); jlabel3.settext(totalageone); // sets jlabel 1 }else{ int totaltwo = 0; totaltwo = totaltwo + 1; string totalagetwo = integer.tostring(totaltwo); jlabel5.settext(totalagetwo); }
you need current value of jlabel, , add 1 it.
jlabel3.settext(""+(integer.parseint(jlabel3.gettext())+1)); currently, variable totalone , totaltwo reset value of 0 ever time runs through conditional statement because local variables.
Comments
Post a Comment