Positioning Labels

0

I'm having trouble adding labels to different positions, relative to a field (% with%). I'd like to put a JTextField before the field, the left, and a second label below the label .

Example:

Itriedtousesomelayoutmanagers,includingJTextField,however,IcouldnotdowhatIwanted.Couldsomeonegivemeatleastadirectionhowtodo?

importjava.awt.Dimension;importjava.awt.EventQueue;importjava.awt.FlowLayout;importjava.awt.GridBagConstraints;importjava.awt.Insets;importjavax.swing.JComponent;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JPanel;importjavax.swing.JTextField;publicclassTesteLabelextendsJFrame{publicstaticvoidmain(String[]args){EventQueue.invokeLater(()->{TesteLabeltesteLabel=newTesteLabel();});}publicTesteLabel(){add(addComponent());setSize(500,200);setVisible(true);setLocationRelativeTo(null);setDefaultCloseOperation(EXIT_ON_CLOSE);}privateJComponentaddComponent(){JPanelpainelPrincipal=newJPanel();JPanelpainel=newJPanel();painel.setLayout(newFlowLayout());JLabellabelLateral=newJLabel("Label Lateral: ");
        JLabel labelInferior = new JLabel("Label Inferior: ");

        JTextField jTextField = new JTextField();

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridy = 1;
        gbc.gridx = 1;
        gbc.gridheight = 1;
        gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.SOUTH;
        gbc.insets = new Insets(5, 5, 5, 5);

        //painel.add(labelLateral, jTextField);
        painel.add(labelLateral);
        painel.add(jTextField);
        painel.add(labelInferior, gbc);
        jTextField.setPreferredSize(new Dimension(200, 25));

        painelPrincipal.add(painel);

        return painelPrincipal;
    }
}
    
asked by anonymous 02.12.2017 / 15:30

1 answer

1

I achieved this way:

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class TesteLabel extends JFrame {

    public static void main(String[] args) {
        EventQueue.invokeLater(TesteLabel::new);
    }

    public TesteLabel() {
        add(addComponent());
        setSize(500, 200);
        setVisible(true);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    private JComponent addComponent() {
        JPanel painelPrincipal = new JPanel();

        JPanel painel = new JPanel();
        painel.setLayout(new GridBagLayout());

        JLabel labelLateral = new JLabel("Label Lateral: ");

        GridBagConstraints gbc1 = new GridBagConstraints();
        gbc1.weightx = 1.0;
        gbc1.weighty = 1.0;
        gbc1.gridx = 1;
        gbc1.gridy = 1;
        painel.add(labelLateral, gbc1);

        JTextField jTextField = new JTextField();

        GridBagConstraints gbc2 = (GridBagConstraints) gbc1.clone();
        gbc2.gridx = 2;
        gbc2.fill = GridBagConstraints.BOTH;
        gbc2.gridwidth = GridBagConstraints.REMAINDER;

        painel.add(jTextField, gbc2);

        JLabel labelInferior = new JLabel("Label Inferior: ");

        GridBagConstraints gbc3 = (GridBagConstraints) gbc1.clone();
        gbc3.gridx = 2;
        gbc3.gridy = 2;
        gbc3.insets = new Insets(5, 5, 5, 5);

        painel.add(labelInferior, gbc3);

        painelPrincipal.add(painel);

        return painelPrincipal;
    }
}

Result:

ThekeyherewastousegridXandgridYascoordinatestopositionthecomponents,andtheconstantGridBagConstraints.REMAINDERtoforcethetextfieldtobethelastitemonthatline.IusedGridBagConstraints.BOTHsothatthetextfieldfillstheentirespaceofthatlinexcolumnwhereitis,otherwiseitwillnotexpand.

However,itisnotpossibletohavethatspaceinthesecondlinewiththislayout,becauseforthis,youwouldneedtohaveacomponentthere,takingupspaceforacolumn3.Sincethepanelonlyhas2columns,inthelastcolumn.

Youcanreadmoreaboutthislayoutinthis oracle tutorial a>.

    
02.12.2017 / 16:21