java - When using null layout in Swing, my components don't appear until I hover on them -
recently i've been getting making simple games swing in java. current project chess-like game, figured start making 8 8 grid jbuttons , go there.
the problem every time run app, half of buttons missing, , appear when hover above them. i've searched problem, of suggestions saying shouldn't use null layout, , should use of layouts in swing. i've seen far, swing layouts pain work (at least when compared android ones), , prefer using absolute positioning now.
also i've found adding panel.repaint(); fixes missing buttons, don't solution since doesn't fix problem more patches it. when running app repaint, still see missing buttons split second until repainted.
so looking why thing happens in first place, , there way not happen without using bad swing layouts.
import javax.swing.*; public class main { static final int tile = 70; static final int width = 600; static final int height = 600; public static void main(string[] args){ jframe frame = new jframe("knight's tour"); frame.setsize(width, height); frame.setresizable(false); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setlocationrelativeto(null); frame.setvisible(true); jpanel panel = new jpanel(); panel.setlayout(null); frame.add(panel); jbutton[][] tiles = new jbutton[8][8]; for(int = 0; < 8; i++){ for(int j = 0; j < 8; j++){ tiles[i][j] = new jbutton(); tiles[i][j].setlocation(i*tile, j*tile); tiles[i][j].setsize(tile, tile); panel.add(tiles[i][j]); } } //panel.repaint(); //this thing fixes it, looking better fix } }
normally 1 last after adding panel , components:
frame.pack(); frame.setvisible(true); the pack() doing layouting on preferred sizes.
one jtable 8x8 too.
Comments
Post a Comment