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
|
public class Controleur{
private MonTimer mt;
private String chaine = "abcdefghijklmnopqrstuvwxyz";
public Controleur() {
String input = JOptionPane.showInputDialog("Combien de caracteres ?", 0);
int i = Integer.parseInt(input);
JFrame fenetre = new JFrame();
JPanel panel = new JPanel();
JButton start = new JButton("start");
JButton stop = new JButton("stop");
start.addActionListener(new Bouton2Listener(mt,i,chaine));
stop.addActionListener(new Bouton1Listener(mt));
panel.add(start);
panel.add(stop);
fenetre.setTitle("Network test");
fenetre.setResizable(true);
fenetre.setLocationRelativeTo(null);
fenetre.setContentPane(panel);
fenetre.setVisible(true);
}
public static void main(String...args){
new Controleur();
}
}
class MonTimer extends Timer{
int n;
String s;
TimerTask task;
public MonTimer(int a, String b){
n = a;
s = b;
task = new TimerTask(){
public void run(){
System.out.println(s.substring(0,n));
}
};
}
public TimerTask getTT(){
return task;
}
}
class Bouton1Listener implements ActionListener{
MonTimer timer;
public Bouton1Listener(MonTimer tt){
timer = tt;
}
public void actionPerformed(ActionEvent arg0) {
timer.cancel();
}
}
class Bouton2Listener implements ActionListener{
MonTimer timer;
int input;
String chaine;
public Bouton2Listener(MonTimer mt,int i,String s){
timer = mt;
input=i;
chaine=s;
}
public void actionPerformed(ActionEvent arg0) {
timer = new MonTimer(input,chaine);
timer.scheduleAtFixedRate(timer.getTT(),0,2000);
}
} |
Partager