一个仿 Eclipse 欢迎窗口的代码 - FormLayout典型示例

一个仿 Eclipse 欢迎窗口的代码

一个背景图片,最下方是一个进度条,上面有一个label,显示一些信息

技术点总结:

一、窗口居中

二、Form布局

三、SWT UI线程调度(本例实现了一个假的),注意到,只有UI线程才能操作UI的控件。


在别的Windows中 new WelcomeWindow().open()即可,此Windows执行完加载任务后会自动关闭。


/**
 * Welcome Window
 */
public class WelcomeWindow {

	//private static Logger logger = LoggerFactory.getLogger(WelcomeWindow.class);

	private Shell shell;

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		configureShell();
		shell.open();
		// shell.layout();

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Configure shell
	 * 
	 * @param shell
	 */
	protected void configureShell() {
		shell.pack();

		Rectangle rctDisplay = shell.getDisplay().getBounds();
		Rectangle rctShell = shell.getBounds();
		int x = (rctDisplay.width - rctShell.width) / 2;
		int y = (rctDisplay.height - rctShell.height) / 2;
		shell.setLocation(x, y);
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell(SWT.ON_TOP);
		shell.setLayout(new FillLayout());

		// Composite as container
		Composite container = new Composite(shell, SWT.NONE);
		FormLayout layout = new FormLayout();
		container.setLayout(layout);

		// ProgressBar
		final ProgressBar bar = new ProgressBar(container, SWT.HORIZONTAL);
		bar.setMinimum(0);
		bar.setMaximum(100);
		final int min = bar.getMinimum();
		final int max = bar.getMaximum();

		FormData formData = null;
		formData = new FormData();
		formData.left = new FormAttachment(0, 0);
		formData.right = new FormAttachment(100, 0);
		formData.bottom = new FormAttachment(100, 0);
		bar.setLayoutData(formData);

		// Label Message
		final Label lblMessage = new Label(container, SWT.INHERIT_DEFAULT);
		lblMessage.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_WHITE));
		formData = new FormData();
		formData.left = new FormAttachment(0, 0);
		formData.right = new FormAttachment(60);
		formData.bottom = new FormAttachment(bar, 0);
		lblMessage.setLayoutData(formData);

		// Label Image
		Label lblImage = new Label(container, SWT.NONE);
		lblImage.setImage(Registry.getImage("logo.bmp"));
		formData = new FormData();
		formData.left = new FormAttachment(0, 0);
		formData.top = new FormAttachment(0, 0);
		lblImage.setLayoutData(formData);

		final int step = 5;
		new Thread(new Runnable() {
			public void run() {
				shell.getDisplay().asyncExec(new Runnable() {
					public void run() {
						for (int i = min; i < max; i += step) {
							if (bar.isDisposed()) {
								return;
							}
							try {
								Thread.sleep(100);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
							String text = GlobalVariable.getResourceBundle().getString("ww.bar.loading");
							text = MessageFormat.format(text, bar.getSelection(), StringUtils.repeat('.', i / step));
							lblMessage.setText(text);
							bar.setSelection(bar.getSelection() + i);
						}
						shell.dispose();
					}
				});
			}
		}).start();
	}

}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值