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 96
| import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;
public class LesBoutons extends JPanel implements ActionListener
{
private JButton start = new JButton("Start");
private JButton reset = new JButton("Reset");
private JButton stop = new JButton("Stop");
boolean compt = false;
public LesBoutons()
{
//ardoise = new Cadre();
setLayout(new BorderLayout(5, 5));
JPanel lesBoutons = new JPanel();
lesBoutons.add(start);
lesBoutons.add(stop);
lesBoutons.add(reset);
add(lesBoutons, BorderLayout.NORTH);
// add(ardoise, BorderLayout.CENTER);
start.addActionListener(this);
stop.addActionListener(this);
reset.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource().equals(start))
{
Thread t = new Thread()
{
public void run()
{
LesBoutons.this.start();
}
};
t.start();
} else if (e.getSource() == reset)
{
setCompt(false);
System.out.println("recommencer");
}
//ardoise.arret();
else if (e.getSource() == stop)
{
setCompt(false);
System.out.println("stop");
}
//ardoise.reset();
// ardoise.repaint();
}
private synchronized void setCompt(boolean b)
{
compt = false;
}
private void start()
{
compt = true;
while (isCompt())
{
// ardoise.run();
System.out.println("demarré");
/*
* // Ici on va imaginer que tu doivens changer un label ou une
* progressbar, donc il faut un invokeLater pour toucher les element
* de swing a l'interieur d'un thread qui n'est pas l'EDT.
* SwingUtilities.invokeLater(new Runnable()
* { public void run() {
* jLabel.setText("balbalbla"); } });
*/
try
{
Thread.sleep(1000);
} catch (InterruptedException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
// ardoise.run();
}
}
private synchronized boolean isCompt()
{
return compt;
}
} |