Incompréhension read And Dispatch
J'ai codé une petit une petite horloge ou qd on modifie les heures ou minutes l'heure clignote. et au moment ou je retourne dans le mode normal qui affiche l'heure ... je me suis aperçu que display.readAndDispatch était toujours égale à true. Je ne comprend pas du tout pourquoi ??
Code:
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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import org.eclipse.swt.*;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.*;
public class Fenetre {
Text text;
Display display;
Group group;
String s;
private Watch watch;
public Fenetre(Watch watch) {
this.watch = watch;
display = new Display();
Shell shell = new Shell(display);
shell.setText("Watch");
shell.addListener(SWT.Close, new Listener() {
public void handleEvent(Event event) {
System.exit(0);
}
});
group = new Group(shell, SWT.NONE);
group.setText("Show Time");
group.setLayout(new FillLayout());
text = new Text(group, SWT.NONE);
text.setEditable(false);
text.setBounds(10, 30, 100, 25);
text.setText("12:46");
/* ActionListener taskPerformer = new ActionListener() {
/*public void actionPerformed(ActionEvent arg0) {
if (display.isDisposed())
return;*/
final Runnable timer = new Runnable() {
public void run() {
if (text.getVisible()) {
System.out.println("false");
text.setVisible(false);
} else
text.setVisible(true);
display.timerExec(500, this);
}
};
/*
* text.getDisplay().syncExec(new Runnable() { public void run()
* { if (text.getVisible()) { System.out.println("false");
* text.setVisible(false); } else text.setVisible(true); } });
*/
//}
//};
// final Timer clignotement = new Timer(500, taskPerformer);
Button button1 = new Button(group, SWT.NONE);
button1.setText("Mode");
button1.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
System.out.println("Appui sur le bouton MODE");
if (getWatch().getMode() == Mode.SHOWTIME) {
group.setText("Adjust hours");
display.timerExec(500, timer);
// clignotement.start();
} else if (getWatch().getMode() == Mode.ADJUSH) {
group.setText("Adjust minutes");
} else {
group.setText("Show time");
// clignotement.stop();
display.timerExec(-1, timer);
text.setVisible(true);
}
getWatch().getA().click("A");
}
});
Button button2 = new Button(group, SWT.NONE);
button2.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
getWatch().getB().click("B");
}
});
button2.setText("Set");
group.pack();
shell.pack();
shell.open();
while (!shell.isDisposed()) {
shell.update();
if (!display.readAndDispatch()) {
display.sleep();
}
this.print();
}
display.dispose();
}
public void print() {
text.setText(watch.getHour() + ":" + watch.getMinute());
}
public Watch getWatch() {
return this.watch;
}
} |
merci d'avance.
Bonne soirée!!