public class SplashScreen extends Shell { private ProgressBar loadingbar; /** * Create the shell * @param display */ public SplashScreen(Display display) { super(display, SWT.ON_TOP); createContents(); } /** * Create contents of the window */ protected void createContents() { final FormLayout layout = new FormLayout(); this.setLayout(layout);
loadingbar = new ProgressBar(this, SWT.NONE); final FormData progressData = new FormData(); progressData.left = new FormAttachment(0, 0); progressData.right = new FormAttachment(100, 0); progressData.bottom = new FormAttachment(100, 0); loadingbar.setLayoutData(progressData);
final Label imageLabel = new Label(this, SWT.NONE); imageLabel.setImage(ImageHolder.getImage(ImageHolder.IMAGE_SPLASH)); final FormData labelData = new FormData(); labelData.right = new FormAttachment(100, 0); labelData.bottom = new FormAttachment(100, 0); imageLabel.setLayoutData(labelData); } @Override protected void checkSubclass() { // Disable the check that prevents subclassing of SWT components }
public void closeAll (){ ImageHolder.getInstance().dispose(); }
/** * * @return ProgressBar */ public ProgressBar getLoadingbar() { return loadingbar; } public void setLoadingBar (int maximum){ loadingbar.setMaximum(maximum); } }public class TestSplash{ public static void main(String args[]) { try { final Display display = Display.getDefault(); final SplashScreen splash = new SplashScreen(display); splash.setLoadingBar(15); splash.pack(); splash.open(); splash.getDisplay().asyncExec(new Runnable(){ public void run() { for (int i = 0; i < 15; i++){ splash.getLoadingbar().setSelection(i); try { Thread.sleep(300); } catch (Exception e) { } } splash.close(); splash.closeAll(); } }); while (!splash.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } } catch (Exception e) { e.printStackTrace(); } } }这样就ok了
public static void main(String[] args) { final Display display = new Display(); final int[] count = new int[] { 1 }; final Image image = new Image(display, 400, 300); GC gc = new GC(image); gc.setBackground(display.getSystemColor(SWT.COLOR_CYAN)); gc.fillRectangle(image.getBounds()); gc.drawText("Splash Screen", 10, 10); gc.dispose(); final Shell splash = new Shell(SWT.ON_TOP); final ProgressBar bar = new ProgressBar(splash, SWT.NONE); bar.setMaximum(count[0]); Label label = new Label(splash, SWT.NONE); label.setImage(image); FormLayout layout = new FormLayout(); splash.setLayout(layout); FormData labelData = new FormData(); labelData.right = new FormAttachment(100, 0); labelData.bottom = new FormAttachment(100, 0); label.setLayoutData(labelData); FormData progressData = new FormData(); progressData.left = new FormAttachment(0, 5); progressData.right = new FormAttachment(100, -5); progressData.bottom = new FormAttachment(100, -5); bar.setLayoutData(progressData); splash.pack(); Rectangle splashRect = splash.getBounds(); Rectangle displayRect = display.getBounds(); int x = (displayRect.width - splashRect.width) / 2; int y = (displayRect.height - splashRect.height) / 2; splash.setLocation(x, y); splash.open(); display.asyncExec(new Runnable() { public void run() { Shell[] shells = new Shell[count[0]]; for (int i = 0; i < count[0]; i++) { shells[i] = new Shell(display); shells[i].setSize(300, 300); shells[i].addListener(SWT.Close, new Listener() { public void handleEvent(Event e) { --count[0]; } }); bar.setSelection(i + 1); try { Thread.sleep(1000); } catch (Throwable e) { } } splash.close(); image.dispose(); for (int i = 0; i < count[0]; i++) { shells[i].open(); } } }); while (count[0] != 0) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
主程序继续运行,完成了大量的初始化准备工作之后,发一个消息给阻塞的进程,告诉他可以结束了,
然后就可以setVisiable了,
/**
* Create the shell
* @param display
*/
public SplashScreen(Display display) {
super(display, SWT.ON_TOP);
createContents();
} /**
* Create contents of the window
*/
protected void createContents() {
final FormLayout layout = new FormLayout();
this.setLayout(layout);
loadingbar = new ProgressBar(this, SWT.NONE);
final FormData progressData = new FormData();
progressData.left = new FormAttachment(0, 0);
progressData.right = new FormAttachment(100, 0);
progressData.bottom = new FormAttachment(100, 0);
loadingbar.setLayoutData(progressData);
final Label imageLabel = new Label(this, SWT.NONE);
imageLabel.setImage(ImageHolder.getImage(ImageHolder.IMAGE_SPLASH));
final FormData labelData = new FormData();
labelData.right = new FormAttachment(100, 0);
labelData.bottom = new FormAttachment(100, 0);
imageLabel.setLayoutData(labelData);
} @Override
protected void checkSubclass() {
// Disable the check that prevents subclassing of SWT components
}
public void closeAll (){
ImageHolder.getInstance().dispose();
}
/**
*
* @return ProgressBar
*/
public ProgressBar getLoadingbar() {
return loadingbar;
} public void setLoadingBar (int maximum){
loadingbar.setMaximum(maximum);
}
}public class TestSplash{
public static void main(String args[]) {
try {
final Display display = Display.getDefault();
final SplashScreen splash = new SplashScreen(display);
splash.setLoadingBar(15);
splash.pack();
splash.open();
splash.getDisplay().asyncExec(new Runnable(){ public void run() {
for (int i = 0; i < 15; i++){
splash.getLoadingbar().setSelection(i);
try {
Thread.sleep(300);
} catch (Exception e) {
}
}
splash.close();
splash.closeAll();
}
});
while (!splash.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}这样就ok了
final Display display = new Display();
final int[] count = new int[] { 1 };
final Image image = new Image(display, 400, 300);
GC gc = new GC(image);
gc.setBackground(display.getSystemColor(SWT.COLOR_CYAN));
gc.fillRectangle(image.getBounds());
gc.drawText("Splash Screen", 10, 10);
gc.dispose();
final Shell splash = new Shell(SWT.ON_TOP);
final ProgressBar bar = new ProgressBar(splash, SWT.NONE);
bar.setMaximum(count[0]);
Label label = new Label(splash, SWT.NONE);
label.setImage(image);
FormLayout layout = new FormLayout();
splash.setLayout(layout);
FormData labelData = new FormData();
labelData.right = new FormAttachment(100, 0);
labelData.bottom = new FormAttachment(100, 0);
label.setLayoutData(labelData);
FormData progressData = new FormData();
progressData.left = new FormAttachment(0, 5);
progressData.right = new FormAttachment(100, -5);
progressData.bottom = new FormAttachment(100, -5);
bar.setLayoutData(progressData);
splash.pack();
Rectangle splashRect = splash.getBounds();
Rectangle displayRect = display.getBounds();
int x = (displayRect.width - splashRect.width) / 2;
int y = (displayRect.height - splashRect.height) / 2;
splash.setLocation(x, y);
splash.open();
display.asyncExec(new Runnable() {
public void run() {
Shell[] shells = new Shell[count[0]];
for (int i = 0; i < count[0]; i++) {
shells[i] = new Shell(display);
shells[i].setSize(300, 300);
shells[i].addListener(SWT.Close, new Listener() {
public void handleEvent(Event e) {
--count[0];
}
});
bar.setSelection(i + 1);
try {
Thread.sleep(1000);
} catch (Throwable e) {
}
}
splash.close();
image.dispose();
for (int i = 0; i < count[0]; i++) {
shells[i].open();
}
}
});
while (count[0] != 0) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}