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


一个仿 Eclipse 欢迎窗口的代码,一个背景图片,最下方是一个进度条,上面有一个label,显示一些信息

技术点总结:

一、窗口居中

二、Form布局

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

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

  1. /** 
  2.  * Welcome Window 
  3.  */  
  4. public class WelcomeWindow {  
  5.   
  6.     //private static Logger logger = LoggerFactory.getLogger(WelcomeWindow.class);   
  7.   
  8.     private Shell shell;  
  9.   
  10.     /** 
  11.      * Open the window. 
  12.      */  
  13.     public void open() {  
  14.         Display display = Display.getDefault();  
  15.         createContents();  
  16.         configureShell();  
  17.         shell.open();  
  18.         // shell.layout();   
  19.   
  20.         while (!shell.isDisposed()) {  
  21.             if (!display.readAndDispatch()) {  
  22.                 display.sleep();  
  23.             }  
  24.         }  
  25.     }  
  26.   
  27.     /** 
  28.      * Configure shell 
  29.      *  
  30.      * @param shell 
  31.      */  
  32.     protected void configureShell() {  
  33.         shell.pack();  
  34.   
  35.         Rectangle rctDisplay = shell.getDisplay().getBounds();  
  36.         Rectangle rctShell = shell.getBounds();  
  37.         int x = (rctDisplay.width - rctShell.width) / 2;  
  38.         int y = (rctDisplay.height - rctShell.height) / 2;  
  39.         shell.setLocation(x, y);  
  40.     }  
  41.   
  42.     /** 
  43.      * Create contents of the window. 
  44.      */  
  45.     protected void createContents() {  
  46.         shell = new Shell(SWT.ON_TOP);  
  47.         shell.setLayout(new FillLayout());  
  48.   
  49.         // Composite as container   
  50.         Composite container = new Composite(shell, SWT.NONE);  
  51.         FormLayout layout = new FormLayout();  
  52.         container.setLayout(layout);  
  53.   
  54.         // ProgressBar   
  55.         final ProgressBar bar = new ProgressBar(container, SWT.HORIZONTAL);  
  56.         bar.setMinimum(0);  
  57.         bar.setMaximum(100);  
  58.         final int min = bar.getMinimum();  
  59.         final int max = bar.getMaximum();  
  60.   
  61.         FormData formData = null;  
  62.         formData = new FormData();  
  63.         formData.left = new FormAttachment(00);  
  64.         formData.right = new FormAttachment(1000);  
  65.         formData.bottom = new FormAttachment(1000);  
  66.         bar.setLayoutData(formData);  
  67.   
  68.         // Label Message   
  69.         final Label lblMessage = new Label(container, SWT.INHERIT_DEFAULT);  
  70.         lblMessage.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_WHITE));  
  71.         formData = new FormData();  
  72.         formData.left = new FormAttachment(00);  
  73.         formData.right = new FormAttachment(60);  
  74.         formData.bottom = new FormAttachment(bar, 0);  
  75.         lblMessage.setLayoutData(formData);  
  76.   
  77.         // Label Image   
  78.         Label lblImage = new Label(container, SWT.NONE);  
  79.         lblImage.setImage(Registry.getImage("logo.bmp"));  
  80.         formData = new FormData();  
  81.         formData.left = new FormAttachment(00);  
  82.         formData.top = new FormAttachment(00);  
  83.         lblImage.setLayoutData(formData);  
  84.   
  85.         final int step = 5;  
  86.         new Thread(new Runnable() {  
  87.             public void run() {  
  88.                 shell.getDisplay().asyncExec(new Runnable() {  
  89.                     public void run() {  
  90.                         for (int i = min; i < max; i += step) {  
  91.                             if (bar.isDisposed()) {  
  92.                                 return;  
  93.                             }  
  94.                             try {  
  95.                                 Thread.sleep(100);  
  96.                             } catch (InterruptedException e) {  
  97.                                 e.printStackTrace();  
  98.                             }  
  99.                             String text = GlobalVariable.getResourceBundle().getString("ww.bar.loading");  
  100.                             text = MessageFormat.format(text, bar.getSelection(), StringUtils.repeat('.', i / step));  
  101.                             lblMessage.setText(text);  
  102.                             bar.setSelection(bar.getSelection() + i);  
  103.                         }  
  104.                         shell.dispose();  
  105.                     }  
  106.                 });  
  107.             }  
  108.         }).start();  
  109.     }  
  110.   
  111. }  

相关内容