Today a simple SWT timer, with a stopwatch timer, to achieve the pause and continue the function, see the following detailed code:
Package com.wild.HomeWork02;
Import Org.eclipse.swt.widgets.Display;
Import Org.eclipse.swt.widgets.Shell;
Import Org.eclipse.swt.widgets.Label;
Import Org.eclipse.swt.SWT;
Import Org.eclipse.swt.widgets.Text;
Import Org.eclipse.swt.widgets.Button;
Import Org.eclipse.swt.events.SelectionAdapter;
Import org.eclipse.swt.events.SelectionEvent;
Import Org.eclipse.wb.swt.SWTResourceManager;
Import Org.eclipse.swt.events.MouseAdapter;
Import org.eclipse.swt.events.MouseEvent;
Import Org.eclipse.swt.events.DisposeListener;
Import org.eclipse.swt.events.DisposeEvent;
public class MyTimer implements Runnable {protected Shell shell;
private text text;
private static Label label_2;
private static Label label_5;
private static Label Label_time_hour1;
private static Label label_time_hour2;
private static Label label_time_min1;
private static Label label_time_min2;
private static Label label_time_sed1;
private static Label Label_time_sed2;
private static Button button_1; Private statIC Button button_2;
private static Boolean flag = FALSE;
private static String numstr;
private int i;
private int num;
public static int H1;
public static int H2;
public static int M1;
public static int m2;
public static int S1;
public static int S2;
/** * Launch the application.
* * @param args */public static void main (string[] args) {try {mytimer window = new MyTimer ();
window.open ();
catch (Exception e) {e.printstacktrace ();
}/** * Open the window.
* * public void Open () {Display display = Display.getdefault ();
Createcontents ();
Shell.open ();
Shell.layout ();
while (!shell.isdisposed ()) {if (!display.readanddispatch ()) {display.sleep ();
}}/** * Create contents of the window.
*/protected void createcontents () {shell = new shell ();
Shell.setsize (450, 300);
Shell.settext ("SWT application"); Label label = new label (Shell, SWT.
NONE);
Label.setbounds (97, 35, 43, 17); Label.settext ("Time:"); Text = new text (Shell, SWT.
BORDER);
Text.setforeground (Swtresourcemanager.getcolor (153, 153, 153));
Text.settext ("unit: Seconds");
Text.setbounds (146, 32, 115, 23); Button button = New button (Shell, SWT.
NONE);
Button.setbounds (102, 77, 43, 27);
Button.settext ("Start"); Label_time_hour1 = new Label (Shell, SWT.
NONE);
Label_time_hour1.setimage (Swtresourcemanager.getimage (Mytimer.class, "/images/0.gif"));
Label_time_hour1.setbounds (97, 130, 21, 32); LABEL_TIME_HOUR2 = new Label (Shell, SWT.
NONE);
Label_time_hour2.setimage (Swtresourcemanager.getimage (Mytimer.class, "/images/0.gif"));
Label_time_hour2.setbounds (124, 130, 21, 32); label_2 = new Label (Shell, SWT.
NONE); Label_2.setfont (Swtresourcemanager.getfont ("Microsoft yahei UI", SWT.)
BOLD));
Label_2.settext (":");
Label_2.setbounds (156, 130, 21, 32); Label_time_min1 = new Label (Shell, SWT.
NONE);
Label_time_min1.setimage (Swtresourcemanager.getimage (Mytimer.class, "/images/0.gif")); Label_time_min1.setBounds (183, 130, 21, 32); label_time_min2 = new Label (Shell, SWT.
NONE);
Label_time_min2.setimage (Swtresourcemanager.getimage (Mytimer.class, "/images/0.gif"));
Label_time_min2.setbounds (210, 130, 21, 32); Label_5 = new Label (Shell, SWT.
NONE);
Label_5.settext (":"); Label_5.setfont (Swtresourcemanager.getfont ("Microsoft yahei UI", SWT.)
BOLD));
Label_5.setbounds (237, 130, 21, 32); label_time_sed1 = new Label (Shell, SWT.
NONE);
Label_time_sed1.setimage (Swtresourcemanager.getimage (Mytimer.class, "/images/0.gif"));
Label_time_sed1.setbounds (264, 130, 21, 32); Label_time_sed2 = new Label (Shell, SWT.
NONE);
Label_time_sed2.setimage (Swtresourcemanager.getimage (Mytimer.class, "/images/0.gif"));
Label_time_sed2.setbounds (291, 130, 21, 32); button_1 = new Button (Shell, SWT.
NONE);
Button_1.setbounds (167, 77, 43, 27);
Button_1.settext ("suspend"); button_2 = new Button (Shell, SWT.
NONE);
Button_2.setbounds (234, 77, 51, 27);
Button_2.settext ("continue");
Click Start Timer Button.addselectionlistener (New Selectionadapter () {@Override public void widgetselected (Selectionevent e) {N
Umstr = Text.gettext (). Trim ();
Mytimer.flag = true;
Start thread MyTimer my = new MyTimer ();
Thread tr = new Thread (my);
Tr.start ();
}
}); Click on the Suspend thread Button_1.addselectionlistener (new Selectionadapter () {@Override public void widgetselected (Selectioneve
NT e) {Mytimer.flag = false;
int hour1 = MYTIMER.H1;
int hour2 = MYTIMER.H2;
int min1 = MYTIMER.M1;
int min2 = mytimer.m2;
int sed1 = MYTIMER.S1;
int sed2 = MYTIMER.S2;
Mytimer.numstr = String. valueof (HOUR1 * + hour2) * (min1 * + min2) * + sed1 * + sed2);
SYSTEM.OUT.PRINTLN (S1 + "= =" + S2 + "= =" + mytimer.numstr);
}
}); Click on the thread to continue Button_2.addselectionlistener (new Selectionadapter () {@Override public void widgetselected (Selectioneve
NT e) {Mytimer.flag = true; Start thread MyTimer my = new MyTimer ();
Thread tr = new Thread (my);
Tr.start ();
}
}); Mouse clicks 0 Text.addmouselistener (New Mouseadapter () {@Override public void MouseDown (MouseEvent e) {Text.sett
Ext ("");
Text.setforeground (swtresourcemanager.getcolor (0, 0, 0));
}
}); Destroy panel Shell.adddisposelistener (new Disposelistener () {public void widgetdisposed (Disposeevent arg0) {SYSTEM.E
XIT (0);
}
});
@Override public void Run () {while (mytimer.flag) {num = integer.valueof (MYTIMER.NUMSTR);
for (i = num. i > 0; i--) {if (Mytimer.flag = = False) {return;
try {thread.sleep (1000);
catch (Interruptedexception e) {e.printstacktrace (); }//Shared area Display.getdefault (). Asyncexec (New Runnable () {@Override public void run () {if (num/ (60 * 60))
> 0) {mytimer.h1 = (I/(60 * 60)% 100)/10;
MYTIMER.H2 = I/(60 * 60)% 10; MYTIMER.M1 = ((i% (60 * 60))% 10)/60;//Draw Mytimer.m2 = (i% (60 * 60)/60)% 10;
MYTIMER.S1 = (i% 60)/10;
MYTIMER.S2 = (i% 60)% 10;
Label_time_hour1.setimage (Swtresourcemanager.getimage (Mytimer.class, String.Format ("/images/%s.gif", H1));
Label_time_hour2.setimage (Swtresourcemanager.getimage (Mytimer.class, String.Format ("/images/%s.gif", H2)); Label_time_min1.setimage (Swtresourcemanager.getimage (Mytimer.class, String.Format ("/images/%s.gif", M1))
;
Label_time_min2.setimage (Swtresourcemanager.getimage (Mytimer.class, String.Format ("/images/%s.gif", m2));
Label_time_sed1.setimage (Swtresourcemanager.getimage (Mytimer.class, String.Format ("/images/%s.gif", S1));
Label_time_sed2.setimage (Swtresourcemanager.getimage (Mytimer.class, String.Format ("/images/%s.gif", S2));
else if ((NUM/60) > 0) {int m1 = I/60/10;
int m2 = (i/60)% 10;
int S1 = ((i% 60)/10); int s2 = (I%)% 10;//=40 label_time_min1.setimage (Swtresourcemanager.getimage (Mytimer.class, String.Format ("I/I
Mages/%s.gif ", M1));
Label_time_min2.setimage (Swtresourcemanager.getimage (Mytimer.class, String.Format ("/images/%s.gif", m2));
Label_time_sed1.setimage (Swtresourcemanager.getimage (Mytimer.class, String.Format ("/images/%s.gif", S1));
Label_time_sed2.setimage (Swtresourcemanager.getimage (Mytimer.class, String.Format ("/images/%s.gif", S2));
else {int S1 = I/10;
int s2 = i% 10;
Label_time_sed1.setimage (Swtresourcemanager.getimage (Mytimer.class, String.Format ("/images/%s.gif", S1));
Label_time_sed2.setimage (Swtresourcemanager.getimage (Mytimer.class, String.Format ("/images/%s.gif", S2));
}
}
});
}
}
}
}