java - Increment an object name within a loop -
i using loop create grid of 60 jbuttons. want have name of actionlistener increment each time loops runs:
for (int y = 0; y < game.getheight(); y++) { (int x = 0; x < game.getwidth(); x++) { jbutton square = new jbutton(); square.setfont(new font("verdana", font.plain, 20)); actionlistener bh = new button_handler(); square.addactionlistener(bh); grid.add(square); }
so name increment (bh_1, bh_2, bh_3, etc).
thanks!
if goal able keep track of actionlisteners
name after creation, want use array this. (if not why want name them differently, need more info.)
assume have actionlistener[60]
called actionlisteners
, counter called buttoncount
.
jbutton square = new jbutton(); square.setfont(new font("verdana", font.plain, 20)); actionlistener bh = new button_handler(); actionlisteners[buttoncount++] = bh; // store handler in array later square.addactionlistener(bh); grid.add(square);
now able access actionlisteners
array.
Comments
Post a Comment