Reputation: 19
When I run my code the JFrame Window shows the smallest possible version of the title frame, it needs to be resized in order to actually show anything. Usually this is due to the lack of a pack() or setVisible(true) at the end, but not in this case. This is the code:
public static void main(String[] args) throws FileNotFoundException {
Boolean[][] maze = Exercise4.readMaze();
int row = maze.length;
JFrame f = new JFrame("Maze");
f.setLayout(new GridLayout(row, row));
for (int i = 0; i < row; i++) {
for (int j = 0; j < row; j++) {
JLabel grid = new JLabel();
grid.setOpaque(true);
if (maze[i][j].equals(false))
grid.setBackground(Color.black);
else grid.setBackground(Color.white);
f.add(grid);
}
}
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
Upvotes: 1
Views: 158
Reputation: 347194
JLabel grid = new JLabel();
is creating a blank label, it's default size will 0x0
, so when you pack
the frame, it's been packed down to preferred size of, essentially 0x0
Consider using something like JLabel grid = new JLabel(" ");
to start with, but I might consider using a "blank" icon of a specified size instead
You could also use some text and set the foreground
(text) color to the same color as the background, but I'd personally use a transparent icon, but that's me
Upvotes: 2
Reputation:
Try creating a JPanel
and placing all the components within it, then adding the JPanel
to the JFrame
.
Upvotes: 0