Bonjour,
J'ai un JPanel qui suivant une certaine valeur contient des composants différents.
Lorsque j'appuie sur un bouton, je voudrai que ça passe de l'un à l'autre.
Malheureusement, je n'y arrive pas.
Voici un petit code qui simule ce que je veux faire.
Le but est d'arriver à remplir la méthode refreshPanel.
Merci d'avance
Alain![]()
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
82
83
84
85 import java.awt.Color; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.WindowConstants; public class TestFrame extends JFrame { private JLabel label1; private JLabel label2; private JLabel label3; private JButton button1; private JButton button2; private JPanel mainPanel; private boolean switcher = true; public TestFrame(){ super(); initGUI(); getContentPane().add(mainPanel); } private void initGUI(){ System.out.println("### initGUI()"); mainPanel = initMainPanel(); } private JPanel initMainPanel() { System.out.println("### initMainPanel()"); JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT,10,10)); if (switcher){ label1 = new JLabel("LABEL 1"); label1.setForeground(Color.ORANGE); panel.add(label1); label2 = new JLabel("LABEL 2"); panel.add(label2); button1 = new JButton("BUTTON 1"); panel.add(button1); button1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) { refreshPanel(); } }); switcher=false; }else{ label3 = new JLabel("LABEL 3"); label3.setForeground(Color.BLUE); panel.add(label3); button2 = new JButton("BUTTON 2"); panel.add(button2); button2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) { refreshPanel(); } }); switcher=true; } return panel; } private void refreshPanel(){ // ???? } public static void main(String[] args) { TestFrame frame = new TestFrame(); frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); frame.setBounds(200,200,250,150); frame.setVisible(true); } }
Partager