Bonjour,
Je débute en Java et j'essaye de me former en SWT et à l'utilisation des StackLayout. J'ai regardé le cours de Mickael Baron, l'exemple sur les StackLayout fonctionne chez moi sans problème.
J'ai essayé de "corser" en utilisant des radio button qui doivent faire apparaître un label suivant le bouton choisit. Je n'arrive pas à comprendre pourquoi l'affichage ne se fait pas correctement.
Voici mon code
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
86
87
88
89
90
91
92
93
94
95 import org.eclipse.swt.SWT; import org.eclipse.swt.custom.StackLayout; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Group; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; public class MainClass { Display m_Display = null; Shell m_Shell = null; StackLayout m_StackLayout = null; Button m_Button1 = null; Button m_Button2 = null; Label m_Label1 = null; Label m_Label2 = null; Group m_GroupLabel = null; public MainClass() { m_Display = new Display(); m_Shell = new Shell(m_Display); m_StackLayout = new StackLayout(); m_Shell.setLayout(new GridLayout(1,false)); Group lGroupButton = new Group(m_Shell,SWT.NONE ); lGroupButton.setLayout(new GridLayout(2,false)); m_Button1 = new Button(lGroupButton,SWT.RADIO); m_Button1.setText("Test 1"); m_Button1.setSize(150, 40); m_Button2 = new Button(lGroupButton,SWT.RADIO); m_Button2.setText("Test 2"); m_Button2.setSize(150, 40); m_GroupLabel = new Group(m_Shell,SWT.NONE ); m_GroupLabel.setLayout(m_StackLayout); m_Label1 = new Label(m_GroupLabel,SWT.NONE); m_Label1.setText("Je vois le 1"); m_Label2 = new Label(m_GroupLabel,SWT.NONE); m_Label2.setText("Je vois le 2"); m_Button1.addSelectionListener(new DisplayBouton()); m_Button2.addSelectionListener(new DisplayBouton()); m_StackLayout.topControl = m_Label1; m_Shell.pack(); m_Shell.open(); while (!m_Shell.isDisposed()) { if (!m_Display.readAndDispatch()) m_Display.sleep(); } m_Display.dispose(); } class DisplayBouton implements SelectionListener { public void widgetSelected(SelectionEvent arg0) { Button lButton = (Button)arg0.getSource(); if (lButton.getText().equals("Je vois le 1")) { m_StackLayout.topControl = m_Label2; m_GroupLabel.redraw(); } else { m_StackLayout.topControl = m_Label1; m_GroupLabel.redraw(); } m_Shell.redraw(); } public void widgetDefaultSelected(SelectionEvent arg0){ } } public static void main (String[] args) { MainClass lMainWindow = new MainClass(); } }
Merci d'avance pour votre aide.
Partager