Samples > スプラッシュウィンドウを表示する

 

スプラッシュウィンドウを表示する

スプラッシュウィンドウを表示するには、ShellのコンストラクタにSWT.ON_TOPを指定します。またウィンドウを画面の中央に配置するには、下のようなイディオムを使うとよいでしょう。

Display display = new Display(...);
Shell splash = new Shell(SWT.ON_TOP);
...
splash.pack();
//スプラッシュウィンドウを中央に配置
Rectangle shellRect = splash.getBounds();
Rectangle dispRect = display.getBounds();
int x = (dispRect.width - shellRect.width) / 2;
int y = (dispRect.height - shellRect.height) / 2;
splash.setLocation(x, y); //位置の指定はpack()のあとに呼ぶ必要がある
splash.open();
...
//アプリケーションの初期化等の処理
...
splash.dispose();

サンプルプログラム

サンプルを起動すると画面中央にスプラッシュウィンドウが表示されます。しばらく待った後、メインのShellが表示されます。

ソースコード (SplashWindow.java)

splash.gif

import org.eclipse.swt.widgets.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.*;

public class SplashWindow {

        private Display display;
        
        public static void main(String[] args) {
                new SplashWindow();
        }
        
        public SplashWindow() {
                display = new Display();
                openSplashWindow();

                Shell shell = new Shell(display);
                shell.setText("SplashWindow");
                shell.setSize(200, 100);
                shell.open();

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

        private void openSplashWindow() {
                Shell splash = new Shell(SWT.ON_TOP);
                splash.setText("SplashWindow");
                splash.setLayout(new GridLayout(1, false));
                Image img = new Image(display, "splash.gif");
                Label label = new Label(splash, SWT.NONE);
                label.setImage(img);

                Label loadLabel = new Label(splash, SWT.NONE);
                GridData gd = new GridData(GridData.FILL_HORIZONTAL);
                loadLabel.setLayoutData(gd);

                splash.pack();

                //スプラッシュウィンドウを中央に配置
                Rectangle shellRect = splash.getBounds();
                Rectangle dispRect = display.getBounds();
                int x = (dispRect.width - shellRect.width) / 2;
                int y = (dispRect.height - shellRect.height) / 2;
                //位置の指定はpack()のあとに呼ぶ必要がある
                splash.setLocation(x, y);
                splash.open();

                for (int i = 0; i < 10; i++) {
                        loadLabel.setText("読み込み中 ... " + i * 10 + "%");
                        try {
                                Thread.sleep(300);
                        } catch (InterruptedException e) {
                                e.printStackTrace();
                        }
                }
                loadLabel.setText("完了");
                img.dispose();
                splash.dispose();
        }
}


最新更新日: 2004年2月17日
 
関連リンク
Eclipse API ドキュメント
API - Shell
Shell
Image

- PR -

マイクロソフト お得な見積! まとめての購入ならオトクな方法で。ライセンスだから管理も簡単。

シマンテック 割引価格! オンライン販売だから低価格。いつでも簡単、見積・購入。

■原石のままでは、終わりたくない貴方へ!キャリアアップ転職を成功へとナビゲート

秋葉原・なんば・名古屋・札幌に店舗を構えるパソコンショップ!【ツクモ】


Copyright(C) 2003,2004 Jasmin Project. All Right Reserved.
SEO [PR] 爆速!無料ブログ 無料ホームページ開設 無料ライブ放送