Bonjour,
Je vous explique mon problème de débutant. J'ai crée un petit formulaire avec un bouton, lorsque je clique dessus je veux qu'une fonction se lance jusqu'à ce que je reclique dessus. D'après ce que j'ai lu, il faut utiliser un thread.
Le but étant de mettre une petite IHM à un crawler que j'ai réalisé.
Voila le code actuel :
Lorsque je reclique sur le bouton, rien ne se passe Pourquoi ?
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 import java.awt.BorderLayout; import javax.swing.*; import java.awt.*; import java.awt.event.*; public final class MyCrawler extends JFrame implements ActionListener { /** * @param args */ private JButton button; private JTextField text; private JLabel label; private JFrame frame; private boolean isRunning = false; private Thread tache; public MyCrawler() { this.frame = new JFrame(); this.frame.setSize(400,200); this.frame.setTitle("CrawlerWeb"); this.label = new JLabel("WebCrawler"); this.label.setHorizontalAlignment(JLabel.CENTER); this.label.setFont(new Font("Verdana", Font.BOLD, 20)); this.button = new JButton("Start / Stop"); this.button.addActionListener(this); this.text = new JTextField(); this.frame.getContentPane().add(label,BorderLayout.NORTH); this.frame.getContentPane().add(text,BorderLayout.CENTER); this.frame.getContentPane().add(button,BorderLayout.SOUTH); this.frame.setVisible(true); } public void actionPerformed(ActionEvent e){ // Inverse la valeur de la variable (en route/arret) this.isRunning = !this.isRunning; if(this.isRunning){ System.out.println("C'est parti !"); this.tache = new Thread(){ public void run(){ startCrawling(); } }; this.tache.start(); } else{ this.tache.interrupt(); System.out.println("On s'arrête !"); } } public static void main(String[] args) { try{ // Init thread courant Thread main = Thread.currentThread(); // On donne un nom au thread courant main.setName("myMainThread"); // Début de main System.out.println("début du thread " + main.getName()); new MyCrawler(); //startCrawling(); // fin de main System.out.println("fin du thread " + main.getName()); } catch(Exception e){ e.printStackTrace(); } } public static void startCrawling(){ while(true){ System.out.println("ok"); } } }
D'avance merci de m'aiguiller !
Partager