I'm trying to position some components on a screen, but it's not working very well. So I created a simple example to illustrate the problem. I accept submissions to perfect the way I'm doing.
Note:WheneverIaddapanelinsideatab,Icannotgetittostayneartheedgeoftheflap,itisalwaysfaraway.
packagelayout;importjava.awt.BorderLayout;importjava.awt.Color;importjava.awt.Dimension;importjava.awt.EventQueue;importjava.awt.GridBagConstraints;importjava.awt.GridBagLayout;importjava.awt.Insets;importjavax.swing.BorderFactory;importjavax.swing.JComponent;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JPanel;importjavax.swing.JTabbedPane;importjavax.swing.JTextField;publicclassTesteLayoutextendsJFrame{publicJPaneljpCampos=newJPanel();privatefinalJTabbedPanetabbedPane=newJTabbedPane();privatefinalMeuJTextFielda=newMeuJTextField("");
private final MeuJTextField b = new MeuJTextField("");
private final MeuJTextField c = new MeuJTextField("");
private final MeuJTextField d = new MeuJTextField("");
private final MeuJTextField e = new MeuJTextField("");
private final MeuJTextField aa = new MeuJTextField("");
private final MeuJTextField bb = new MeuJTextField("");
private final MeuJTextField cc = new MeuJTextField("");
private final MeuJTextField dd = new MeuJTextField("");
private final MeuJTextField ee = new MeuJTextField("");
class MeuJTextField extends JTextField {
public MeuJTextField(String nome) {
//setColumns(120);
setText("teste de layouts");
}
}
public TesteLayout() {
add(adicionaPaines());
//setSize(600, 300);
pack();
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private JComponent adicionaPaines() {
JPanel adicionaPaines = new JPanel();
adicionaPaines.setLayout(new GridBagLayout());
JPanel painel = new JPanel();
JLabel label = new JLabel("Painel superior");
painel.add(label);
JPanel painel2 = new JPanel();
JLabel label2 = new JLabel("Aqui vai um label");
painel2.add(label2);
label2.setForeground(new Color(99, 49, 131));
adicionaComponente(1, 1, 1, 1, painel, adicionaPaines);
adicionaComponente(2, 1, 1, 1, painel2, adicionaPaines);
adicionaComponente(3, 1, 1, 1, adionaAba(), adicionaPaines);
return adicionaPaines;
}
private JComponent adionaAba() {
JPanel painelAbas = new JPanel();
painelAbas.setLayout(new GridBagLayout());
tabbedPane.addTab("Teste", tela());
tabbedPane.setPreferredSize(new Dimension(550, 250));
add(tabbedPane, BorderLayout.BEFORE_LINE_BEGINS);
return painelAbas;
}
private JComponent tela() {
JPanel tela = new JPanel();
JPanel painel1 = new JPanel();
painel1.setLayout(new GridBagLayout());
painel1.setBorder(BorderFactory.createTitledBorder("Painel 01"));
adicionaComponente(1, 1, 1, 1, painel1, tela);
adicionaComponente(2, 1, 1, 1, a, painel1);
adicionaComponente(3, 1, 1, 1, b, painel1);
adicionaComponente(4, 1, 1, 1, c, painel1);
adicionaComponente(4, 3, 1, 1, d, painel1);
adicionaComponente(5, 1, 1, 1, e, painel1);
JPanel painel2 = new JPanel();
painel2.setLayout(new GridBagLayout());
painel2.setBorder(BorderFactory.createTitledBorder("Painel 02"));
adicionaComponente(1, 1, 1, 1, painel2, tela);
adicionaComponente(2, 1, 1, 1, aa, painel2);
adicionaComponente(3, 1, 1, 1, bb, painel2);
adicionaComponente(4, 1, 1, 1, cc, painel2);
adicionaComponente(4, 3, 1, 1, dd, painel2);
adicionaComponente(5, 1, 1, 1, ee, painel2);
return tela;
}
public void adicionaComponente(int linha, int coluna, int linhas, int colunas, JComponent componente, JPanel painel) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridy = linha;
gbc.gridx = coluna;
gbc.gridheight = 1;
gbc.gridwidth = 1;
gbc.anchor = GridBagConstraints.EAST;
gbc.insets = new Insets(5, 5, 5, 5); // ↑, ←, ↓, →
if (painel == null) {
painel = jpCampos;
}
gbc.anchor = GridBagConstraints.WEST;
gbc.gridx++;
gbc.gridheight = linhas;
gbc.gridwidth = colunas;
painel.add(componente, gbc);
}
public static void main(String[] args) {
EventQueue.invokeLater(()
-> {
TesteLayout layout = new TesteLayout();
layout.setVisible(true);
});
}
}