java - removeMouseListener method not working -
the removemouselistener not working: if click on old jlabel still adding variable score. every time time lapse passed old object should have mouselistener
removed , new 1 should 1 it.
timer pictimer = new timer(1000, new actionlistener(){ int oldrr=0; int oldrc=0 ; final int[] score = {0}; @override public void actionperformed(actionevent e) { mouseadapter act = new mouseadapter(){ @override public void mouseclicked(mouseevent e){ score[0]++; lbltimer.settext(string.valueof(score[0])); } }; arraywm[oldrr][oldrc].removemouselistener(act); arraywm[oldrr][oldrc].seticon(null); random random = new random(); arraywm[oldrr][oldrc].seticon(null); int rr = random.nextint(3 - 0 + 1) + 0; int rc = random.nextint(3 - 0 + 1) + 0; oldrr = rr; oldrc = rc; arraywm[rr][rc].seticon(new imageicon("img/one.jpg")); arraywm[rr][rc].addmouselistener(act ); } });
each time timer
triggered, create instance of mouseadapter
, attempt remove instance jlabel
, label not have instance registered it. add instance of mouselistener
label, compounding number of mouselistener
s registered 1 label @ time.
instead, create single instance of mouseadapter
, re-use, maybe this...
timer pictimer = new timer(1000, new actionlistener() { int oldrr = 0; int oldrc = 0; final int[] score = {0}; mouseadapter act = new mouseadapter() { @override public void mouseclicked(mouseevent e) { score[0]++; lbltimer.settext(string.valueof(score[0])); } }; @override public void actionperformed(actionevent e) { arraywm[oldrr][oldrc].removemouselistener(act); arraywm[oldrr][oldrc].seticon(null); random random = new random(); arraywm[oldrr][oldrc].seticon(null); int rr = random.nextint(3 - 0 + 1) + 0; int rc = random.nextint(3 - 0 + 1) + 0; oldrr = rr; oldrc = rc; arraywm[rr][rc].seticon(new imageicon("img/one.jpg")); arraywm[rr][rc].addmouselistener(act); } });
Comments
Post a Comment