今天做了个简单的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() {
@Override
public 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() {
@Override
public 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() {
@Override
public void widgetSelected(SelectionEvent e) {
MyTimer.flag = true;
// 启动线程
MyTimer my = new MyTimer();
Thread tr = new Thread(my);
tr.start();
}
});
// 鼠标点击清零
text.addMouseListener(new MouseAdapter() {
@Override
public 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);
}
});
}
@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();
}
// 共享区
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;// 得出
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;// =40
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 {
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)));
}
}
});
}
}
}
}