0

I would like a form with 2 elements on a row (JLabel and JTextField), but I don't know used layouts in Java (Swing).

I know that GridBagLayout is a good Layout for this. Someone people can help me?

public class FenetreAjoutClient {

public JPanel FenetreAjoutClient(){
    panel.add(lblNom);
    panel.add(txtNom);
    panel.add(lblPrenom);
    panel.add(txtPrenom);
    panel.add(lblAdr);
    panel.add(txtAdr);
    panel.add(lblCP);
    panel.add(txtCP);
    panel.add(lblVille);
    panel.add(txtVille);
    panel.add(lblStatut);
    comboStatut.addItem("Particulier");
    comboStatut.addItem("Professionnel");
    panel.add(comboStatut);
    panel.add(lblRemise);
    panel.add(txtRemise);
    panel.add(lblSaisieObl);

    //bnEnregistrer.setEnabled(false);
    bnEnregistrer.setPreferredSize(new Dimension(140, 40));
    bnEnregistrer.setBackground(new Color(50, 74, 110));
    bnEnregistrer.setForeground(Color.WHITE);
    bnEnregistrer.addMouseListener(this);
    panel.add(bnEnregistrer);

    bnAnnuler.setPreferredSize(new Dimension(140, 40));
    bnAnnuler.setBackground(new Color(50, 74, 110));
    bnAnnuler.setForeground(Color.WHITE);
    bnAnnuler.addMouseListener(this);
    panel.add(bnAnnuler);


    JLabel labels[] = {lblNom, lblPrenom, lblAdr, lblCP, lblVille, lblStatut, lblRemise};
    JTextField txtFields[] = {txtNom, txtPrenom, txtAdr, txtCP, txtVille, txtRemise};
    for(int i = 0; i < labels.length && i < txtFields.length; i++){
        labels[i].setPreferredSize(new Dimension(110, 20));
        txtFields[i].setPreferredSize(new Dimension(160, 20));
    }
    //Composants particuliers
    txtAdr.setPreferredSize(new Dimension(230, 20));
    txtCP.setPreferredSize(new Dimension(50, 20));
    txtRemise.setPreferredSize(new Dimension(50, 20));

    activeBouton();

    return panel;
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
b-user
  • 97
  • 1
  • 3
  • 10
  • 1
    1) The best strategy with layouts is usually to combine them, using whatever layout works best for each logical section of the GUI. Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height. 2) But for a simple label/field combo. either `GridBagLAyout` or (if using an IDE with GUI builder) `GroupLayout` would be good. 3) See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) .. – Andrew Thompson Jun 08 '17 at 13:53
  • .. In this case, labels take their preferred size from the text and font. Text fields get a size from the number of columns and the font. Buttons get their size from the size of the text / icon and the margins. Don't try to guess what it should be. 4) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Jun 08 '17 at 13:59
  • `I know that GridBagLayout is a good Layout for this.` - ok, then use it. Start by reading the section from the Swing tutorial on [How to Use GridBagLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html) for basic information and working examples. We are not going to write the code for you. – camickr Jun 08 '17 at 14:09

0 Answers0