java - Board's squares wont appear. -
i have 3 classes: 1 main:
public class thera { public static void main(string[] args) { theragui start = new theragui(); } }
one gui
public class theragui extends jframe { public theragui(){ board board = new board(); this.setlayout(new gridbaglayout()); this.settitle("thera"); this.setdefaultcloseoperation(jframe.exit_on_close); this.setvisible(true); this.add(board); this.pack(); } }
and last board:
package thera; import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; public class board extends jpanel { jlayeredpane layeredpane; jpanel board; jlabel piece; int xadj; int yadj; private static final string imagefolderpath = "src/resources/images/"; dimension dimension = new dimension(500, 500); public board(){ //create layered pane layeredpane = new jlayeredpane(); layeredpane.setpreferredsize(dimension); this.add(layeredpane); //create board board = new jpanel(); board.setlayout(new gridlayout(7,9)); board.setpreferredsize(dimension); layeredpane.add(board, jlayeredpane.default_layer); for(int c = 0; c < 7; c++){ for(int r = 0; r < 9; r++){ jpanel square = new jpanel(); square.setlayout(new borderlayout()); square.setbackground(new color(255, 204, 051)); board.add(square); } } } }
my problem: board won't appear! additional info: primary layout gridbaglayout, , in cell 1 added jpanel, inside jpanel added layeredpane.
you're adding component (thus changing hierarchy) after setting visibility. swing kinda pedantic how things work.
public class theragui extends jframe { public theragui(){ board board = new board(); this.setlayout(new gridbaglayout()); this.settitle("thera"); this.setdefaultcloseoperation(jframe.exit_on_close); // note order this.add(board); this.pack(); this.setvisible(true); } }
Comments
Post a Comment