SWT写的秒表程序

     最近做毕业设计,是基于量子进化算法的对弈游戏,我感觉在毕业设计上加个秒表比较好,于是写了一个。

SWT写的秒表程序

图1

//StopWatch.java
import java.awt.Dimension;
import java.awt.Toolkit;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import com.swtdesigner.SWTResourceManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
public class StopWatch {
	protected Shell shell;
	private Text theTime;
	static StopWatch watch=null;
	private boolean startted=false;
	private boolean paused=false;
	private long startTime=0;
	private long pauseTime=0;
	private long allPauseTime=0;
	TimeCount t=null;
	public static void main(String[] args) {
		try {
			watch = new StopWatch();
			watch.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}
	protected void createContents() {
		shell = new Shell(SWT.MIN);
		shell.setImage(SWTResourceManager.getImage(StopWatch.class, "/javax/swing/plaf/basic/icons/JavaCup16.png"));
		shell.setBackground(SWTResourceManager.getColor(102, 102, 255));
		shell.setSize(243, 130);
		shell.setText("秒表");
		Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
		int w=d.width;
		int h=d.height;
		shell.setBounds((w-shell.getSize().x)/2, (h-shell.getSize().y)/4, shell.getSize().x, shell.getSize().y);
		final Button start = new Button(shell, SWT.NONE);
		start.setBounds(36, 63, 72, 22);
		start.setText("开始");
		Button reset = new Button(shell, SWT.NONE);
		reset.setBounds(127, 63, 72, 22);
		reset.setText("清零");
		theTime = new Text(shell, SWT.BORDER | SWT.READ_ONLY | SWT.CENTER);
		theTime.setBackground(SWTResourceManager.getColor(102, 102, 255));
		theTime.setFont(SWTResourceManager.getFont("Times New Roman", 27, SWT.BOLD | SWT.ITALIC));
		theTime.setText("00:00:00.000");
		theTime.setBounds(10, 10, 217, 47);
		start.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if(startted==false){
					if(!paused){
						startTime=System.currentTimeMillis();
						pauseTime=startTime;
					}
					allPauseTime+=System.currentTimeMillis()-pauseTime;
					t=new TimeCount(startTime,allPauseTime,watch);
					t.start();
					startted=true;
					paused=false;
					start.setText("暂停");
				}
				else{
					pauseTime=System.currentTimeMillis();
					start.setText("开始");
					paused=true;
					t.stop();
					startted=false;
				}
			}
		});
		reset.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(t!=null)
					t.stop();
				startted=false;
				paused=false;
				allPauseTime=0;
				updateTime("00:00:00.000");
			}
		}); 
		shell.addShellListener(new ShellAdapter(){
			public void shellClosed(ShellEvent e) {
				if(t!=null){
					t.stop();
				}	
			}
		});
	}
	public void updateTime(final String str){
		Display.getDefault().asyncExec(new Runnable(){
			public void run(){
				theTime.setText(str);
			}
		});
	}
}

 

//TimeCount.java
public class TimeCount extends Thread{
	private long startTime;
	private long msec,sec,minute,hour;
	private String str;
	private long allPauseTime;
	private StopWatch watch;
	public TimeCount(long startTime,long allPauseTime,StopWatch watch){
		this.startTime=startTime;
		this.watch=watch;
		this.allPauseTime=allPauseTime;
	}
	public void run(){
		while(true){
			msec=System.currentTimeMillis()-startTime-allPauseTime;
			sec=msec/1000;
			msec%=1000;
			minute=sec/60;
			sec%=60;
			hour=minute/60;
			minute%=60;
			str=String.format("%02d:%02d:%02d.%03d", hour, minute, sec, msec);   
			watch.updateTime(str);
			 try {   
	             sleep(1);
	         } catch (InterruptedException e) {   
	             e.printStackTrace();   
	         }  
		}
	}
}

  

你可能感兴趣的:(eclipse,thread,游戏,算法,swing)