Position JPanels vertically

1

I'm trying to add 4 panels so that they stay one below the other. So I decided to use BorderLayout , along with "positioning" ( NORTH , SOUTH and etc), passing index , however, it ends up jumping the second panel.

Does BorderLayout not allow putting components in the same "placement"?

ex: 2 panels with CENTER orientation.

import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class NewClass extends JFrame {

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

    public NewClass() {
        add(painel());
        setLocationRelativeTo(null);
        pack();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    private JPanel painel() {
        JPanel painel = new JPanel();
        painel.setLayout(new BorderLayout());

        JPanel painel01 = new JPanel();
        painel01.add(new JLabel("Painel 01"));
        painel01.setPreferredSize(new Dimension(100, 50));

        JPanel painel02 = new JPanel();
        painel02.add(new JLabel("Painel 02"));
        painel02.setPreferredSize(new Dimension(100, 50));

        JPanel painel03 = new JPanel();
        painel03.add(new JLabel("Painel 03"));
        painel03.setPreferredSize(new Dimension(100, 50));

        JPanel painel04 = new JPanel();
        painel04.add(new JLabel("Painel 04"));
        painel04.setPreferredSize(new Dimension(100, 50));

        painel.add(painel01, BorderLayout.NORTH, 0);
        painel.add(painel02, BorderLayout.CENTER, 1);
        painel.add(painel03, BorderLayout.CENTER, 2);
        painel.add(painel04, BorderLayout.SOUTH, 3);
        return painel;
    }
}
    
asked by anonymous 16.09.2017 / 19:53

1 answer

2

If you put 2 components in the same position in BorderLayout , they will occupy the same space inside the container. Pro layout, you are overlapping one over another.

To position the 4 panels inline vertically, you could create only 3 panels, add them to the 3 positions of BorderLayout (north, center and south), and in the center panel add 2 more, but this would only increase the complexity something simple. Perhaps the most appropriate way would be to use the GridLayout , configuring 4 rows and one column:

private JPanel painel() {
    JPanel painel = new JPanel();
    painel.setLayout(new GridLayout(4, 1));

    JPanel painel01 = new JPanel();
    painel01.add(new JLabel("Painel 01"));
    painel01.setPreferredSize(new Dimension(100, 50));

    JPanel painel02 = new JPanel();
    painel02.add(new JLabel("Painel 02"));
    painel02.setPreferredSize(new Dimension(100, 50));

    JPanel painel03 = new JPanel();
    painel03.add(new JLabel("Painel 03"));
    painel03.setPreferredSize(new Dimension(100, 50));

    JPanel painel04 = new JPanel();
    painel04.add(new JLabel("Painel 04"));
    painel04.setPreferredSize(new Dimension(100, 50));

    painel.add(painel01);
    painel.add(painel02);
    painel.add(painel03);
    painel.add(painel04);
    return painel;
}

The result is:

Notice that the constructor of this layout received 2 parameters, where the first is the number of rows and the second of rows.

    
16.09.2017 / 20:10