0

I am new to building GUIs with Java and for my first attempt I decided to create a Minesweeper clone. Everything was going great, until it came to generating the field tiles at run-time. I have a field JPanel (FlowLayout) that is large enough to contain 16*16 mine tiles, where each tile is a JPanel itself (CardLayout consisting of a label and a button on top of it). I managed to get the tiles to show up properly, but I have a small problem. After the 13th tile is added, every next tile moves the whole field panel to the right, along with some components above it, which are not part of the field panel itself. If I add all 256 tiles, the whole field goes somewhere off the screen and so do the components above it.

What is causing this and how can I fix it?

Here are some screenshots of the problem and the form design:

Screenshots

CuriouS
  • 129
  • 1
  • 9
  • 3
    I'd suggest using [GridLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/grid.html) for the field. – kiheru Feb 17 '15 at 10:01
  • 2
    For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). In addition to the advice of @kiheru, see also this [short example](http://stackoverflow.com/a/28420905/418556) that uses `GridLayout` to create a chess board. – Andrew Thompson Feb 17 '15 at 10:05

1 Answers1

2

You have a field JPanel (FlowLayout) that is large enough to contain 16*16 mine tiles. Change this layout to GridLayout. You can see the reference here: http://docs.oracle.com/javase/7/docs/api/java/awt/GridLayout.html

bayuforest
  • 118
  • 2
  • 2
  • 10