java - Add Jbutton to Jpanel -
can tell me wrong code trying add buttons jpanel
arraylist<jbutton> buttons = new arraylist<jbutton>(); jpanel createbuttonspane(){ bpanel = new jpanel(); for(int i=0; i<10; i++){ buttons.add(new jbutton(""+i)); bpanel.add(buttons); } return bpanel; }
this code not compile because jpanel not have overload of add() takes array of jbuttons, can not add whole array of buttons jpanel (even if possible, need outside of for()-loop).
simply add button directly jpanel:
jpanel createbuttonspane(){ bpanel = new jpanel(); for(int i=0; i<10; i++){ bpanel.add(new jbutton(""+i)); } return bpanel; } if still need refer individual jbuttons later, add them array in addition:
jpanel createbuttonspane(){ bpanel = new jpanel(); for(int i=0; i<10; i++){ jbutton button = new jbutton(""+i); buttons.add(button); bpanel.add(button); } return bpanel; }
Comments
Post a Comment