一個仿 Eclipse 歡迎視窗的代碼 – FormLayout典型樣本

來源:互聯網
上載者:User

一個仿 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 containerComposite container = new Composite(shell, SWT.NONE);FormLayout layout = new FormLayout();container.setLayout(layout);// ProgressBarfinal 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 Messagefinal 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 ImageLabel 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();}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.