Bonjour
J'ai une JFrame qui contient 4 Panels positionnés grâce à un BorderLayout: CENTER, WEST, NORTH, SOUTH. Le Panel WEST contient un bouton Ajout qui est censé vider le Panel CENTER et ajouter un nouveau Panel que j'ai nommé nouveauPanel.
Le problème est quand je clique sur le bouton Ajout, rien ne se passe,c'est-à-dire que le Panel CENTER ne m'affiche pas le nouveauPanel.
Voici mon code:
Merci
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package gesstock; import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.CardLayout; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; /** * * @author Mo */ public class Accueil extends JFrame{ JPanel panCentre = new JPanel(); JPanel panHaut = new JPanel(); JPanel panGauche = new JPanel(); JPanel panBas = new JPanel(); JPanel panNouveauPanel = new JPanel(); public Accueil() { this.setSize(700, 500); this.setTitle("Gestion Entreprise"); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ajoutComposant(); this.setVisible(true); } public void ajoutComposant(){ this.setLayout(new BorderLayout()); panHaut.setBackground(Color.red); panHaut.setPreferredSize(new Dimension(0, 60)); panCentre.setBackground(Color.yellow); panCentre.setLayout(new BorderLayout()); panGauche.setPreferredSize(new Dimension(120, 0)); panBas.setBackground(Color.white); panBas.setPreferredSize(new Dimension(0, 40)); JButton ajout = new JButton("Ajouter nouveau panel"); panGauche.add(ajout); panNouveauPanel .setBackground(Color.blue); ajout.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { panCentre.add(panNouveauPanel); } }); //On ajoute les 4 panels au content pane de la JFrame //Au centre this.getContentPane().add(panCentre,BorderLayout.CENTER); //en haut this.getContentPane().add(panHaut,BorderLayout.NORTH); //à gauche this.getContentPane().add(panGauche,BorderLayout.WEST); //en bas this.getContentPane().add(panBas,BorderLayout.SOUTH); } }
Partager