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