GridBagLayout positionements coordonnées
Bonjour, voilà j'ai un soucis c'est par rapport au positionnement de mes Textfields, Labels et Buttons.
Ils ne sont pas aux coordonnées que je souhaite.
Pour les labels nom aux coordonnées (0,0), prenom (1,0) etc.
j’aimerai avoir un panneau à peu prés de ce style
Label_nom text_field_nom
Label_prenom text_field_prenomnom
[Boutton1] [Boutton2]
Je vois pas où est l'erreur ou l'oublie.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Grid
{
public static void main(String[] args)
{
JFrame jframe = new JFrame();
jframe.setTitle("Fiche client");
jframe.setSize(500,500);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setVisible(true);
JLabel label_nom = new JLabel("Nom:") ;
JLabel label_prenom = new JLabel("Prenom:") ;
JTextField text_nom = new JTextField(30);
JTextField text_prenom = new JTextField(30);
JButton button_premier = new JButton("<<");
JButton button_dernier = new JButton(">>");
JPanel panel = new JPanel();
jframe.setContentPane(panel);
panel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.BASELINE_LEADING;
gbc.insets = new Insets(10, 15, 0, 0);
panel.add(label_nom);
gbc.gridx = 1;
gbc.gridy = 0;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.BASELINE;
gbc.insets = new Insets(0, 15, 0, 10);
panel.add(text_nom);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.BASELINE_LEADING;
gbc.insets = new Insets(10, 15, 0, 0);
panel.add(label_prenom);
gbc.gridx = 1;
gbc.gridy = 1;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.BASELINE;
gbc.insets = new Insets(0, 15, 0, 10);
panel.add(text_prenom);
gbc.gridx = 1;
gbc.gridy = 2;
gbc.gridwidth = 2;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.BASELINE_LEADING;
gbc.fill = GridBagConstraints.NONE;
gbc.insets = new Insets(10, 15, 10, 10);
panel.add(button_premier, gbc);
gbc.gridx = 2;
gbc.gridy = 2;
gbc.gridwidth = GridBagConstraints.RELATIVE;
gbc.weightx = 1.;
gbc.anchor = GridBagConstraints.BASELINE_TRAILING;
gbc.insets = new Insets(0, 0, 0, 0);
panel.add(button_dernier, gbc);
}
} |