`

SWT下一個簡單的倒計時窗口

    博客分类:
  • SWT
阅读更多
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class CountDownShell implements Runnable{
	private static final int CountDownTime = 30;	//倒計時,30秒

	protected Shell shell;
	private Thread clocker;
	private Label countLabel;

	
	public static void main(String[] args) {
		try {
			CountDownShell window = new CountDownShell();
			window.open();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	public void open() {
		Display display = Display.getDefault();
		createContents();
		centerShell(display, shell);	//讓窗口居中顯示
		shell.open();
		shell.layout();
		clocker = new Thread(this);
		clocker.start();
		
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}


	protected void createContents() {
		shell = new Shell(SWT.NONE | SWT.APPLICATION_MODAL);
		shell.setLayout(new GridLayout());
		shell.setSize(228, 104);
		shell.setText("SWT Dialog");

		Composite panel = new Composite(shell, SWT.NONE);
		panel.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		panel.setLayout(gridLayout);
		
		final Label label = new Label(panel, SWT.NONE);
		label.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, true));
		label.setText("倒計時:");

		countLabel = new Label(panel, SWT.NONE);
		countLabel.setText("30");

		final Label label_2 = new Label(panel, SWT.NONE);
		label_2.setText("秒");
		
		
	}

	public void run() {
		int i = CountDownTime;	//設置倒計時時間
		
		while(i > 0) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
			i--;
			final int temp = i;
			
			//在其他線程中調用UI線程(即修改界面元素),需要使用Display.getDefault().asyncExec()方法
			Display.getDefault().asyncExec(new Runnable() {
				public void run() {
					countLabel.setText(temp + "");
				}
			});
		}
		
		Display.getDefault().asyncExec(new Runnable() {
			public void run() {
				//倒計時完成,退出窗口。
				shell.dispose();
			}
		});
	}
	
	//居中顯示shell
	private void centerShell(Display display, Shell shell) {
		Rectangle displayBounds = display.getPrimaryMonitor().getBounds();
		Rectangle shellBounds =shell.getBounds();
		int x = displayBounds.x + (displayBounds.width - shellBounds.width)>>1;
		int y = displayBounds.y + (displayBounds.height - shellBounds.height)>>1;
		shell.setLocation(x, y);
	}

}

 

分享到:
评论

相关推荐

    java的swing实现计时与倒计时

    对于“java的swing实现计时与倒计时”这一主题,我们将探讨如何利用Swing构建一个能够输入起始时间和终止时间,并且可以执行计时和倒计时功能的应用程序。 首先,Swing中的JFrame是应用程序的主要窗口,它包含其他...

    Swt的API使用说明

    Swt(Standard Widget Toolkit)是Eclipse基金会开发的一个开源GUI库,主要用于构建原生外观的Java应用程序。Swt API提供了丰富的控件和功能,允许开发者创建与操作系统环境无缝集成的应用界面。这篇文档将详细介绍...

    Java写的秒表 代码+程序

    `org.eclipse.ui.workbench_3.6.2.M20110210-1200.jar`和`org.eclipse.swt.win32.win32.x86_3.6.2.v3659c.jar`是Eclipse工作台和SWT(标准窗口工具包)的库,用于构建图形用户界面(GUI)。这些库表明秒表程序可能...

    用java编的简单俄罗斯方块源码

    本篇将深入探讨一个使用Java语言,结合SWT(Standard Widget Toolkit)和Swing库开发的简单俄罗斯方块游戏的源码,帮助读者理解其中的关键技术和编程思想。 首先,我们要明确Java AWT(Abstract Window Toolkit)和...

Global site tag (gtag.js) - Google Analytics