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