Initially, I have a deck image and the text โDeckโ just below the image, which looks fine

public class GuiTut extends JPanel { private GridBagConstraints c = new GridBagConstraints(); private JLabel deckLabel = new JLabel(); public GuiTut() { setLayout(new GridBagLayout()); try { deck = ImageIO.read(new File("resources/images/deck.jpg")); } catch (Exception e) {} c.gridx = 0; c.gridy = 0; JLabel deckPic = new JLabel(new ImageIcon(deck)); add(deckPic, c); deckLabel.setText("Deck"); c.gridx = 0; c.gridy = 1; add(deckLabel, c); }
But after adding my gridLayout panel gridLayout whole GUI was messed up. Since you can see that the image of my deck is not correctly aligned with the first line of my gridLayout and my deck text has been separated by several wide spaces.

public class GuiTut extends JPanel { private GridBagConstraints c = new GridBagConstraints(); private JLabel deckLabel = new JLabel(); private JPanel gridLayoutPanel = new JPanel(new GridLayout(2, 0)); private JLabel[] label = new JLabel[14]; public GuiTut() { setLayout(new GridBagLayout()); try { card = ImageIO.read(new File("resources/images/card.jpg")); } catch (Exception e) {} for(int i = 0; i < 14; i++) { label[i] = new JLabel(new ImageIcon(card); gridLayoutPanel.add(label[i]); } try { deck = ImageIO.read(new File("resources/images/deck.jpg")); } catch (Exception e) {} c.gridx = 0; c.gridy = 0; JLabel deckPic = new JLabel(new ImageIcon(deck)); add(deckPic, c); deckLabel.setText("Deck"); c.gridx = 0; c.gridy = 1; add(deckLabel, c); c.gridx = 1; c.gridy = 0; add(gridLayoutPanel, c); }
I want the deck image to be aligned with the first gridLayout line and the text "deck" right below the deck image, but I canโt get it.
source share