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;
}
}
None of the components have a set size. Try setting this to see if it solves.
– user28595
I put size, but it didn’t solve !
– Javinha
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
– user28595
can give me an example of this gridlayout ?
– Javinha