vendredi 1 juillet 2016

GridLayout not keeping alignment in JPanel [duplicate]

This question already has an answer here:

Here is the Minimal Working Example or however you wanna call it:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Frame extends JFrame {
    public static final int DEFAULT_WIDTH=400;
    public static final int DEFAULT_HEIGHT=400;
    private JPanel upperPanel;
    private JPanel panel;
    private GridLayout gridLayout;

    public Frame() {
        super("title");
        this.setSize(new Dimension(DEFAULT_WIDTH,DEFAULT_HEIGHT));
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLayout(new BorderLayout());
        createUpperPanel();
        this.add(upperPanel,BorderLayout.NORTH);
        createPanel();
        this.add(panel, BorderLayout.CENTER);
        this.pack();
    }

    public void createUpperPanel() {
        upperPanel=new JPanel();
        upperPanel.setSize(100,100);
        upperPanel.add(new JLabel("head"));
    }

    public void createPanel() {
        panel=new JPanel();
        panel.setSize(200,200);
        gridLayout=new GridLayout(4,4,10,10);
        panel.setLayout(gridLayout);
        addTiles();
    }

    private void addTiles() {
        JLabel label=new JLabel("LABEL");
        label.setSize(new Dimension(40,40));
        label.setOpaque(true);
        label.setBackground(Color.GREEN);

        JLabel label2=new JLabel("LABEL2");
        label.setSize(new Dimension(40,40));
        label.setOpaque(true);
        label.setBackground(Color.YELLOW);

        panel.add(label);
        panel.add(label2);

    }

    public static void main(String[] args) {
        Frame frame=new Frame();
        frame.setVisible(true);
    }

}

If I run this I get ugly results. All I want to do is have a grid of square JLabels with the background color specified containing text. However, the spacing gets messed up and the background color somehow fills the whole row.

Aucun commentaire:

Enregistrer un commentaire