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
|
import java.awt.Color;
import java.awt.Frame;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JToggleButton;
public class Souris extends Frame {
int i=0;
boolean flag;
JToggleButton btnProduction = new JToggleButton("bouton");
Souris(){
setTitle("Test");
setLayout(null);
setBounds(100,100,500,350);
setBackground(Color.cyan);
btnProduction.addItemListener(new ItemListener()
{
@Override
public void itemStateChanged(ItemEvent e)
{
if (btnProduction.isSelected())
{
MonCalcul.start();
} else
{
Moncalcul.stopCalcul();
System.out.println("FINISH");// ... j'arrête mon calcul
}
}
});
btnProduction.setBounds(400,100,50,30);
btnProduction.setBackground(Color.red);
add(btnProduction);
addWindowListener(new FermerFenetre());
setVisible(true);
}
class FermerFenetre extends WindowAdapter{
public void windowClosing(WindowEvent evt){
if(evt.getWindow().getName().equals("frame0")){
System.exit(0);
}else
evt.getWindow().dispose();
}
}
class MonCalcul extends Thread{
public void run(){
flag=true;
while(flag){
System.out.println("i= "+i);
i++;
}
}
public void stopCalcul(){
flag=false;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Souris();
}
} |