Bonsoir . j aimerais que vous m' aidiez sur ce point: comment centrer un menu contenant 4 boutons c'est a dire 2 lignes et 2 colonnes sur un JPanel aidez moi avec un code svp. merci.
Version imprimable
Bonsoir . j aimerais que vous m' aidiez sur ce point: comment centrer un menu contenant 4 boutons c'est a dire 2 lignes et 2 colonnes sur un JPanel aidez moi avec un code svp. merci.
Salut,
Tu peux par exemple utiliser un GridBagLayout et des fillers :
Et si tu veux que les boutons aient tous la même largeur :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 public class MenuCentre { public static void main(String[] args) { JFrame frame = new JFrame("Démo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel inner = new JPanel(new GridBagLayout()); GridBagConstraints gbc=new GridBagConstraints(0,0,1,2,0.25,0,GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,new Insets(1,1,1,1), 0,0); inner.add(new JPanel(),gbc); // filler gbc.gridx=1; gbc.gridheight=1; inner.add(new JButton("Create"),gbc); gbc.gridx=2; inner.add(new JButton("Edit"),gbc); gbc.gridx=3; gbc.gridheight=2; inner.add(new JPanel(),gbc); // filler gbc.gridx=1; gbc.gridy=1; gbc.gridheight=1; inner.add(new JButton("Delete"),gbc); gbc.gridx=2; inner.add(new JButton("Quit"),gbc); frame.add(inner); frame.setSize(400, 400); frame.setVisible(true); } }
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 public class MenuCentre { public static void main(String[] args) { JFrame frame = new JFrame("Démo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel inner = new JPanel(new GridBagLayout()); GridBagConstraints gbc=new GridBagConstraints(0,0,1,2,1,0,GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL,new Insets(1,1,1,1), 0,0); inner.add(new JPanel(),gbc); // filler gbc.gridx=2; inner.add(new JPanel(),gbc); // filler JPanel box1 = new JPanel(new GridLayout(0, 2, 1, 1)); // un gridlayout pour forcer une largeur identique box1.add(new JButton("Create")); box1.add(new JButton("Edit")); box1.add(new JButton("Delete")); box1.add(new JButton("Quit")); gbc.gridx=1; gbc.weightx=0; gbc.fill=GridBagConstraints.NONE; inner.add(box1,gbc); frame.add(inner); frame.setSize(400, 400); frame.setVisible(true); } }
Bonsoir merci à toi Joel. digo c'est exactement ce que je voulais
il y a longtemp parceque je travaillais avec mon code