swing - Java getting the wrong number of jbuttons in grid layout -
so i'm new java , im new swing.
i've got 80 x 80 array thats going used maze. need gui have 80 x 80 buttons can tied values in array.
i cant figure out why i'm getting 5 or 6 large buttons code. if can tell me how can work thank in advance because i'm stumped.
just run , you'll see mean...also guess i've not figured out how change color of buttons , changed background color instead.
heres code:
public static void draw() { jframe f = new jframe(); f.settitle("maze"); f.setsize(800, 800); f.setvisible(true); f.setdefaultcloseoperation(exit_on_close); jpanel c = (jpanel)f.getcontentpane(); gridlayout gridlayout = new gridlayout(); c.setlayout(gridlayout); for(int =0;i<80;i++){ for(int j =0;j<80;j++){ jbutton b = new jbutton(); c.add(b, i,j); b.setsize(10, 10); b.setopaque(true); b.setbackground(color.red); } } } }
80 * 10 > f.setsize(800, 800);
, code not possible fit in fullhd monitor- use
f.pack()
instead off.setsize(800, 800);
- use
f.pack()
,f.setvisible(true);
(could main issue) should last code lines in non_static , renamed !public void drawme() {
!, becausedraw()
reserved word for/in java apic.add(b, i,j);
should last code line (logical ordering),c.add(b, i,j);
set row , columns gridlayout instead of injecting jbutton virtual grid ingridlayout
- make me sense (starting numbers of elements)
from
import java.awt.gridlayout; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.swingutilities; public class drawme { private jframe frame = new jframe(); private jpanel c = new jpanel(); private static final int column = 10; private static final int row = 10; public drawme() { c.setlayout(new gridlayout(row, column)); (int = 0; < column; i++) { (int j = 0; j < row; j++) { jbutton b = new jbutton((i + 1) + " " + (j + 1)); c.add(b); } } frame.add(c); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.pack(); frame.setlocation(150, 150); frame.setvisible(true); } public static void main(string[] args) { swingutilities.invokelater(new runnable() { @override public void run() { new drawme(); } }); } }