今天做了個簡單的swt的計時器,帶有秒錶的計時器,實現了暫停與繼續的功能,下面請看詳細的代碼:
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("時間:");text = new Text(shell, SWT.BORDER);text.setForeground(SWTResourceManager.getColor(153, 153, 153));text.setText("單位:秒");text.setBounds(146, 32, 115, 23);Button button = new Button(shell, SWT.NONE);button.setBounds(102, 77, 43, 27);button.setText("開 始");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", 16, 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", 16, 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("暫 停");button_2 = new Button(shell, SWT.NONE);button_2.setBounds(234, 77, 51, 27);button_2.setText("繼 續");// 點擊開始計時button.addSelectionListener(new SelectionAdapter() {@Overridepublic void widgetSelected(SelectionEvent e) {Numstr = text.getText().trim();MyTimer.flag = true;// 啟動線程MyTimer my = new MyTimer();Thread tr = new Thread(my);tr.start();}});// 點擊暫停線程button_1.addSelectionListener(new SelectionAdapter() {@Overridepublic void widgetSelected(SelectionEvent 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 * 10 + hour2) * 60 * 60 + (min1 * 10 + min2) * 60 + sed1 * 10 + sed2);System.out.println(s1 + "==" + s2 + "==" + MyTimer.Numstr);}});// 點擊線程繼續button_2.addSelectionListener(new SelectionAdapter() {@Overridepublic void widgetSelected(SelectionEvent e) {MyTimer.flag = true;// 啟動線程MyTimer my = new MyTimer();Thread tr = new Thread(my);tr.start();}});// 滑鼠點擊清零text.addMouseListener(new MouseAdapter() {@Overridepublic void mouseDown(MouseEvent e) {text.setText("");text.setForeground(SWTResourceManager.getColor(0, 0, 0));}});// 銷毀面板shell.addDisposeListener(new DisposeListener() {public void widgetDisposed(DisposeEvent arg0) {System.exit(0);}});}@Overridepublic 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();}// 共用區Display.getDefault().asyncExec(new Runnable() {@Overridepublic 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;// 得出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 % 60) % 10;// =40label_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 {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)));}}});}}}}