Bonjour,
Je veux faire un petit programme de sondage où en cliquant sur un bouton, on incrémente un compteur. Tout marche très bien sauf au niveau du réaffichage. Je mets un repaint() à la fin de l'incrémentation, mais le chiffre du nombre de votants ne s'affiche pas. Par contre, si je redimensionne la fenetre, je constate que cela s'affiche très bien.
J'imagine que j'utilise mal repaint mais je ne comprends pas où.
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 import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Sondage extends JFrame implements ActionListener{ Compteur votes= new Compteur(); JPanel fond= new JPanel(); JPanel result=new JPanel(); JFrame fenetre=new JFrame("Sondage"); public Sondage() { super(); fond=(JPanel) fenetre.getContentPane(); fond.add("North", new JLabel ("Etes vous pour : ")); JButton bush= new JButton ("Bush"); fond.add("West", bush); JButton kerry= new JButton ("Kerry"); fond.add("East", kerry); JButton blanc= new JButton ("Blanc"); fond.add("Center", blanc); bush.addActionListener(this); bush.setActionCommand("Bush"); kerry.addActionListener(this); kerry.setActionCommand("Kerry"); blanc.addActionListener(this); blanc.setActionCommand("Blanc"); result.setLayout(new BorderLayout()); result.add("North", new JLabel ("Bush ")); result.add("Center", new JLabel ("Kerry ")); result.add("South", new JLabel ("Blanc ")); fond.add("South", result); fenetre.pack(); fenetre.setVisible(true); } public static void main(String args[]) { Sondage notreFrame = new Sondage(); } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Bush")) { votes.bush(); result.add("East", new JLabel (""+votes.nbVotesBush)); result.repaint(); } if (e.getActionCommand().equals("Kerry")) { votes.kerry(); fond.repaint(); } if (e.getActionCommand().equals("Blanc")) { votes.blanc(); fond.repaint(); } } }
Partager