Placing components inside a JFrame

2

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);
        });
    }
}
    
asked by anonymous 01.05.2017 / 23:20

1 answer

1

You can do this using only BoxLayout :

Passing BoxLayout.Y_AXIS in the constructor of this class, you tell the layout to align the components added along the Y axis, that is, add them vertically. This layout needs to be applied to the main container of JFrame , and then added 3 other subcontainers. The other components you can distribute, as I did in the example, within the 3 main subcontainers.

import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.*;
import javax.swing.border.TitledBorder;

public class MultiPanel extends JFrame {

    private JPanel contentPane;
    private JPanel painelSuperior;
    private JPanel painelLabel;
    private JLabel lblNewLabel;
    private JTabbedPane tabbedPane;
    private JPanel painelTab;
    private JPanel painel1;
    private JPanel panel2;

    public static void main(String[] args) {
        EventQueue.invokeLater(() ->  {
            new MultiPanel().setVisible(true);
        });
    }


    public MultiPanel() {
        initComponents();
    }

    private void initComponents() {

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //este painel será o container principal da tela
        contentPane = new JPanel();
        contentPane.setPreferredSize(new Dimension(400, 250));
        setContentPane(contentPane);
        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));

        painelSuperior = new JPanel();
        painelSuperior.setBorder(new TitledBorder( "Painel Superior"));
        contentPane.add(painelSuperior);

        painelLabel = new JPanel();
        painelLabel.setBorder(new TitledBorder("Painel do Label"));
        contentPane.add(painelLabel);

        lblNewLabel = new JLabel("New label");
        painelLabel.add(lblNewLabel);

        tabbedPane = new JTabbedPane();
        contentPane.add(tabbedPane);

        painelTab = new JPanel();
        tabbedPane.addTab("New tab", painelTab);
        //para que os paineis 1 e 2 fiquem alinhados horizontalmente
        painelTab.setLayout(new BoxLayout(painelTab, BoxLayout.X_AXIS));

        //paineis adicionados a aba do tabbedpane
        painel1 = new JPanel();
        painel1.setBorder(new TitledBorder("painel 1"));
        painelTab.add(painel1);

        panel2 = new JPanel();
        panel2.setBorder(new TitledBorder("painel 2"));
        painelTab.add(panel2);
        pack();
    }
}

Result:

MoredetailsaboutLayoutsManagersandBoxLayoutcanbefoundinthedocumentation:

The Visual Guide to Layout Managers

How to Use BoxLayout

    
02.05.2017 / 00:50