1 pièce(s) jointe(s)
[Swing] Problème avec les GridBagLayout
Salut à tous,
suite à de très nombreux problèmes de placements de composants, je tente de recoder mon interface en utilisant des GridBagLayout. Un bon point, ca marche dans l'ensemble. Le seul truc, c'est que tout les composants sont centrés en hauteur au milieu du panel ou ils sont. Le code est fourni ci dessous:
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
|
// Création du la box contenant les options de recherche
Box searchbox = Box.createVerticalBox();
searchbox.setBorder(new TitledBorder("Criteres de recherche"));
//setSize(searchbox, 240, 450);
this.getContentPane().add(searchbox);
// Barre d'onglets contenant 2 GridBagLayout => 1 container pour chaque onglet
JTabbedPane bar = new JTabbedPane(); // Bar en elle meme
searchbox.add(bar);
// Besoin de deux panels pour les deux onglets
JPanel optCrit = new JPanel();
GridBagLayout GB_optCrit = new GridBagLayout();
optCrit.setLayout(GB_optCrit);
JPanel optAdv = new JPanel();
GridBagLayout GB_optAdv = new GridBagLayout();
optAdv.setLayout(GB_optAdv);
// Ajoute les deux onglets
bar.add("Critères", optCrit);
bar.add("Options", optAdv);
// On arrive au coeur du probleme => Placement des composants
GridBagConstraints GBC_1 = new GridBagConstraints();// Contrainte
GBC_1.gridx = 0;
GBC_1.gridy = 0;
GBC_1.weightx = 0;
GBC_1.weighty = 0;
GBC_1.anchor = GridBagConstraints.NORTHWEST;
GBC_1.insets = new Insets(0,5,5,0);
optCrit.add(new JLabel("Nom du fichier :"), GBC_1);
GridBagConstraints GBC_2 = new GridBagConstraints();// Contrainte
GBC_2.gridx = 0;
GBC_2.gridy = 1;
GBC_2.weightx = 100;
GBC_2.weighty = 0;
GBC_2.anchor = GridBagConstraints.NORTHWEST;
GBC_2.fill = GridBagConstraints.HORIZONTAL;
GBC_2.insets = new Insets(0,0,40,0);
JTextField fileName = new JTextField(33);
optCrit.add(fileName, GBC_2);
GridBagConstraints GBC_3 = new GridBagConstraints();// Contrainte
GBC_3.gridx = 0;
GBC_3.gridy = 5;
GBC_3.weightx = 0;
GBC_3.weighty = 0;
GBC_3.anchor = GridBagConstraints.NORTHWEST;
GBC_3.insets = new Insets(0,5,5,0);
optCrit.add(new JLabel("Nom du fichier :"), GBC_3);
GridBagConstraints GBC_4 = new GridBagConstraints();// Contrainte
GBC_4.gridx = 0;
GBC_4.gridy = 6;
GBC_4.weightx = 100;
GBC_4.weighty = 0;
GBC_4.anchor = GridBagConstraints.NORTHWEST;
GBC_4.fill = GridBagConstraints.HORIZONTAL;
GBC_4.insets = new Insets(0,0,40,0);
JTextField infileName = new JTextField(33);
optCrit.add(infileName, GBC_4);
GridBagConstraints GBC_5 = new GridBagConstraints();// Contrainte
GBC_5.gridx = 0;
GBC_5.gridy = 9;
GBC_5.weightx = 50;
GBC_5.weighty = 0;
GBC_5.fill = GridBagConstraints.HORIZONTAL;
GBC_5.anchor = GridBagConstraints.NORTHWEST;
GBC_5.insets = new Insets(0,0,40,0);
Box hRootsBox = Box.createHorizontalBox();
hRootsBox.add(new JLabel("Source : "));
JComboBox combo = new JComboBox(File.listRoots());
hRootsBox.add(combo);
optCrit.add(hRootsBox, GBC_5); |
Deuxième problème, à quoi correspond le paramètre weightx, weighty, parce qu'il ne semble pas avoir bcp d'influence sur les composants.
Enfin, a mon avis mon troisième probleme est lié à ce dernier. Comment faire pour réduire un peu la taille d'un composant...
Merci bcp!