Layout horizontal et vertical alignement
Salut à tous,
je voudrai créer un panel à l'intérieur duquel j'aurai 2 labels l'un à côté de l'autre.
Je voudrai que ces 2 labels soient toujours aligner au centre verticalement mais je voudrai avoir un contrôle sur l'alignement horizontal des 2 labels : Gauche, Centre et Droite.
J'ai d'abord essayé avec un FlowLayout :
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
|
JPanel panel = new JPanel();
FlowLayout flowLayout = new FlowLayout();
if (serial.getAlignement() == Vertex.LEFT){
flowLayout.setAlignment(FlowLayout.LEFT);
}
else if (serial.getAlignement() == Vertex.CENTER){
flowLayout.setAlignment(FlowLayout.CENTER);
}
else if (serial.getAlignement() == Vertex.RIGHT){
flowLayout.setAlignment(FlowLayout.RIGHT);
}
flowLayout.setVgap(2);
panel.setLayout(flowLayout);
JLabel serialNumberPrefixe = new JLabel();
serialNumberPrefixe.setText("toto");
JLabel serialNumberValue = new JLabel();
serialNumberValue.setText("tata");
panel.add(serialNumberPrefixe);
panel.add(serialNumberValue);
panel.setVisible(true); |
Ceci me permettait d'avoir un contrôle sur l'alignement horizontal de mes 2 labels mais malheureusement je ne pouvais pas spécifier ma volonté de les laisser centrer au niveau de l'alignement vertical.
J'ai donc voulu essayer avec le GridBagLayout :
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
|
GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
gridBagConstraints2.gridy = 0;
gridBagConstraints2.gridx = 1;
GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
gridBagConstraints1.gridy = 0;
gridBagConstraints1.gridx = 0;
jLabel1 = new JLabel();
jLabel1.setText("toto");
jLabel1.setName("jLabel1");
jLabel = new JLabel();
jLabel.setText("tata");
jLabel.setName("jLabel");
jPanel = new JPanel();
jPanel.setLocation(new Point(31, 22));
jPanel.setSize(new Dimension(209, 64));
jPanel.setLayout(new GridBagLayout());
jPanel.setComponentOrientation(ComponentOrientation.UNKNOWN);
jPanel.add(jLabel, gridBagConstraints1);
jPanel.add(jLabel1, gridBagConstraints2); |
Ceci me permet de conserver mes 2 labels centrés verticalement même en cas de redimensionnement du panel malheureusement je n'ai aucun contrôle sur le positionnement horizontal de mes 2 labels (du moins je ne vois pas comment faire).
Quelqu'un peu-t-il m'aider ?
Si vous avez une autre solution n'hésitez pas.
Merci.