Position Jpanels vertically

Asked

Viewed 465 times

1

I am trying to add 4 panels, so that they are below each other. So, I decided to use the BorderLayout, together with the "positioning" (NORTH, SOUTH etc.), pass a index, however, he ends up skipping the second panel.

The BorderLayout not allowed to place components in the same "positioning" ?

ex: 2 panels with orientation CENTER.

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;
    }
}
  • 1

    None of the components have a set size. Try setting this to see if it solves.

  • I put size, but it didn’t solve !

  • 1

    Another obvious detail, you are adding 2 panels in the same location(CENTER), it is expected that one overlaps the other. If you want to distribute several panels, borderlayout is not recommended. Use gridlayout or boxlayout

  • can give me an example of this gridlayout ?

1 answer

2


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

To position the 4 panels in line vertically, you could create only 3 panels, add them in the 3 positions of the BorderLayout(north, center and south), and in the central panel add 2 more, but this would only increase the complexity something simple. Perhaps the most appropriate way would be to use the Gridlayout, setting up 4 rows and a 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:

inserir a descrição da imagem aqui

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

  • So, below each other he doesn’t get this Borderlayout from what Voce said ?

  • @Javinha did not understand.

  • I wanted one bass from the other, "aligned" as in the example I posted, the only problem is that there he did not put the 4. Because as you said he was overlapping. I’ll try with another layout.

  • @Javinha see the edition.

  • our, very good ! thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.