The following code describes JLabels using a GridLayout . The arguments for GridLayout are the following: rows, columns, horizontal clearance, vertical gap. In the example below, I have a width of 3 pixels between the labels both vertically and horizontally.
To use images instead of numbers, you can pass ImageIcon to the ImageIcon constructor instead of text.
However, it looks like you are making a game where the user should be able to click on tiles. This suggests that you might need to use buttons instead of shortcuts, but that is up to you :-)
import java.awt.GridLayout; import javax.swing.*; import javax.swing.border.BevelBorder; public class FrameTest { public static void main(String[] args) { final JFrame f = new JFrame("Frame Test"); JPanel panel = new JPanel(new GridLayout(4, 4, 3, 3)); for (int i = 0; i < 16; i++) { JLabel l = new JLabel("" + i, JLabel.CENTER);

source share