Bonjour,
J'ai un panel dont le layout est un GridBagLayout.
Je lui ajoute un label(0,0) et un textfield(1,0).
Ils apparaissent dans le JPanel centrer verticalement :roll:!
Comment faire pour qu'ils ne le soient plus ?
Merci d'avance.
Version imprimable
Bonjour,
J'ai un panel dont le layout est un GridBagLayout.
Je lui ajoute un label(0,0) et un textfield(1,0).
Ils apparaissent dans le JPanel centrer verticalement :roll:!
Comment faire pour qu'ils ne le soient plus ?
Merci d'avance.
Bonjour,
Montre nous le code correspondant pour de plus amples informations ;)
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 public class NewJFrame extends javax.swing.JFrame { private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JPanel jPanel1; private javax.swing.JTextField jTextField1; private javax.swing.JTextField jTextField2; /** Creates new form NewJFrame */ public NewJFrame() { initComponents(); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ // <editor-fold defaultstate="collapsed" desc=" Generated Code "> private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; jPanel1 = new javax.swing.JPanel(); jLabel2 = new javax.swing.JLabel(); jTextField1 = new javax.swing.JTextField(); jTextField2 = new javax.swing.JTextField(); jLabel1 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jPanel1.setLayout(new java.awt.GridBagLayout()); jLabel1.setText("jLabel1"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 0; jPanel1.add(jLabel1, gridBagConstraints); jTextField1.setText("jTextField1"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 0; gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH; jPanel1.add(jTextField1, gridBagConstraints); jLabel2.setText("jLabel2"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 1; jPanel1.add(jLabel2, gridBagConstraints); jTextField2.setText("jTextField2"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 1; jPanel1.add(jTextField2, gridBagConstraints); getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER); java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); setBounds((screenSize.width-400)/2, (screenSize.height-306)/2, 400, 306); }// </editor-fold> /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new NewJFrame().setVisible(true); } }); } }
tout doit se jouer sur cette ligne là :
suffit certainement de changer le CENTERCode:
1
2getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
ça marche ... mais c'est mal foutu quand même !!!
Si on peut plus mettre un panel au centre d'une frame ... sans que ça influe sur l'alignement des composants à l'intérieur !!
Sinon pour le gridBag il faut jouer avec la composante anchor des contraints et la placer en NORTHWEST (ou WESTselon le résultat voulu) ou encore LEADING