歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 一個仿 Eclipse 歡迎窗口的代碼 - FormLayout典型示例

一個仿 Eclipse 歡迎窗口的代碼 - FormLayout典型示例

日期:2017/3/1 10:14:01   编辑:Linux編程

一個仿 Eclipse 歡迎窗口的代碼,一個背景圖片,最下方是一個進度條,上面有一個label,顯示一些信息

技術點總結:

一、窗口居中

二、Form布局

三、SWT UI線程調度(本例實現了一個假的),注意到,只有UI線程才能操作UI的控件。

在別的Windows中 new WelcomeWindow().open()即可,此Windows執行完加載任務後會自動關閉。

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