java - Creating live counter within a JFrame -
i tried searching sort of interactive jlabel
. want this:
i'm not sure called or find info on displaying this. want print refreshed number when +/- pressed. have working print on eclipse console unsure how print jframe
.
here of code:
string input = joptionpane.showinputdialog("please enter number of laps."); numlaps = integer.parseint(input); //frame creation jframe f = new jframe("number of laps"); f.setsize(550, 450); f.setdefaultcloseoperation(jframe.exit_on_close); f.setlayout(null); // label creation jlabel label = new jlabel("you entered " + numlaps + " laps. press + add lap. press - subtract lap.", swingconstants.center); label.setbounds(0,0,500,300); f.add(label); //display window f.setvisible(true); //button creation add jbutton add = new jbutton ("+"); add.setbounds(350,250,50,50); add.setpreferredsize(new dimension(50,50)); add.addactionlistener( new actionlistener() { @override public void actionperformed(actionevent e) { // happens when button pressed //numlaps++; addtolap(); system.out.println(numlaps); } }); f.add(add); //button creation subtract jbutton sub = new jbutton ("-"); sub.setbounds(100,250,50,50); sub.setpreferredsize(new dimension(50,50)); sub.addactionlistener( new actionlistener() { @override public void actionperformed(actionevent e) { // happens when button pressed //numlaps--; subtolap(); system.out.println(numlaps); } }); f.add(sub);
& add/sub methods:
private static void addtolap() { // todo auto-generated method stub numlaps++; } private static void subtolap() { // todo auto-generated method stub numlaps--; }
you try following:
private int numlaps = 0 // or number want initialize
then in methods should assing numlaps value jlabel this:
this.mylabel.settext(string.valueof(numlaps));
i hope helps.
Comments
Post a Comment