swing Splash Screen

關於登錄以前的那個歡迎屏幕,不少人都是用一個JWindow來使用,其實java自己具有了這樣的機制,關於使用JWindow能夠看看我那個Frame透明和形狀也能夠達到一個好的效果。不過JVM提供的比較好,內容以下。java

主要來自於http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/splashscreen/動畫

可是哪裏的代碼好有點問題,個人是jdk7。不知道是否是這個緣由,我修改以後的代碼。this

//http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/splashscreen/orm

public class SplashTest extends Frame implements ActionListener {圖片

 

    static void renderSplashFrame(Graphics2D g, int frame) {get

        final String[] comps = {"foo", "bar", "baz"};it

        g.setComposite(AlphaComposite.Clear);io

        String str = "Loading " + comps[(frame / 5) % 3] + "...";form

        g.fillRect(465, 384, 355, 250);class

        g.setPaintMode();

 

        g.setColor(Color.WHITE);          /*清空上一次存留   這幾個座標可能和分辨率有關,這個只是我機器分辨率,直接複製代碼可能有問題*/

        g.fillRect(130, 200 - 10, 150, 14);

 

        g.setColor(Color.red);           //紅色進度

        g.fillRect(130, 200 - 10, (frame * 10) % 280, 14);

 

        g.setColor(Color.BLACK);           //進度信息

        g.drawString(str, 130, 200);

    }

 

    public SplashTest() {

        super("SplashScreen demo");

        setSize(500, 300);

        setLayout(new BorderLayout());

        Menu m1 = new Menu("File");

        MenuItem mi1 = new MenuItem("Exit");

        m1.add(mi1);

        mi1.addActionListener(this);

 

        MenuBar mb = new MenuBar();

        setMenuBar(mb);

        mb.add(m1);

        final SplashScreen splash = SplashScreen.getSplashScreen();

        if (splash == null) {

            System.out.println("SplashScreen.getSplashScreen() returned null");

            return;

        }

        Graphics2D g = (Graphics2D) splash.createGraphics();

        System.out.println("splash rect   :" + splash.getBounds());

        System.out.println("splash size    :" + splash.getSize());

        if (g == null) {

            System.out.println("g is null");

            return;

        }

        for (int i = 0; i < 10; i++) {

            splash.update();

            renderSplashFrame(g, i);

            try {

                Thread.sleep(500);

            } catch (InterruptedException e) {

            }

        }

        splash.close();

        setVisible(true);

        toFront();

    }

 

    public void actionPerformed(ActionEvent ae) {

        System.exit(0);

    }

 

    public static void main(String args[]) {

        SplashTest test = new SplashTest();

    }

}

注意點:

1.須要在manifest.mf文件里加上

Main-Class: SplashTest    //主類

SplashScreen-Image: splashtest_350W.gif  //圖片,能夠使添加動畫圖片,圖片目錄和java文件是同一個目錄。

2.不能在IDE裏運行,須要打包jar以後運行纔會有效果。

相關文章
相關標籤/搜索