用Swing製作歡迎畫面

來源:互聯網
上載者:User
幾乎所有時髦的應用都有一個歡迎畫面。歡迎畫面既是宣傳產品的方法之一,而且在長時間的應用啟動過程中,歡迎畫面還用來表示應用正在準備過程中。

下面是一個最簡單的歡迎畫面實現:

class SplashWindow1 extends JWindow

{

    public SplashWindow1(String filename, Frame f)

    {

        super(f);

        JLabel l = new JLabel(new ImageIcon(filename));

        getContentPane().add(l, BorderLayout.CENTER);

        pack();

        Dimension screenSize =

          Toolkit.getDefaultToolkit().getScreenSize();

        Dimension labelSize = l.getPreferredSize();

        setLocation(screenSize.width/2 - (labelSize.width/2),

                    screenSize.height/2 - (labelSize.height/2));

        setVisible(true);

        screenSize = null;

        labelSize = null;

    }

}

SplashWindow1類從Swing的JWindow派生。JWindow是一個容器,它沒有其他視窗所具有的各種視窗元素,如標題條、視窗管理按鈕,甚至連反白的邊框也沒有。因此,JWindow對於製作歡迎畫面來說是非常合適的。上面的代碼假定圖形檔案在目前的目錄。圖形通過ImageIcon裝入記憶體,然後它就被放到了JWindow的中心。接著,視窗被pack(),這使得Swing把視窗調整到適當的大小,最後視窗被移到了螢幕的中心。

如果我們運行上面的程式,可以發現雖然歡迎畫面確實出現在螢幕中央,但遺憾的是,它卻不會關閉!要關閉歡迎畫面,我們需要加入更多的代碼:

class SplashWindow2 extends JWindow

{

    public SplashWindow2(String filename, Frame f)

    {

        super(f);

        JLabel l = new JLabel(new ImageIcon(filename));

        getContentPane().add(l, BorderLayout.CENTER);

        pack();

        Dimension screenSize =

          Toolkit.getDefaultToolkit().getScreenSize();

        Dimension labelSize = l.getPreferredSize();

        setLocation(screenSize.width/2 - (labelSize.width/2),

                    screenSize.height/2 - (labelSize.height/2));

        addMouseListener(new MouseAdapter()

            {

                public void mousePressed(MouseEvent e)

                {

                    setVisible(false);

                    dispose();

                }

            });

        setVisible(true);

    }

}

和原先的SplashWindow1類相比,這個SplashWindow2類唯一的區別在於多出了一個安裝到JWindow上的匿名MouseListener。經過這個改動之後,使用者可以點擊歡迎畫面關閉它。

現在我們有了一個很不錯的歡迎畫面,它可以通過點擊的方法關閉,但它不會自己消失。接下來我們要加入代碼,使得歡迎畫面在顯示一定的時間之後自動消失。這裡我們要考慮到運用線程。

class SplashWindow3 extends JWindow

{

    public SplashWindow3(String filename, Frame f, int waitTime)

    {

        super(f);

        JLabel l = new JLabel(new ImageIcon(filename));

        getContentPane().add(l, BorderLayout.CENTER);

        pack();

        Dimension screenSize =

          Toolkit.getDefaultToolkit().getScreenSize();

        Dimension labelSize = l.getPreferredSize();

        setLocation(screenSize.width/2 - (labelSize.width/2),

                    screenSize.height/2 - (labelSize.height/2));

        addMouseListener(new MouseAdapter()

            {

                public void mousePressed(MouseEvent e)

                {

                    setVisible(false);

                    dispose();

                }

            });

        final int pause = waitTime;

        final Runnable closerRunner = new Runnable()

            {

                public void run()

                {

                    setVisible(false);

                    dispose();

                }

            };

        Runnable waitRunner = new Runnable()

            {

                public void run()

                {

                    try

                        {

                            Thread.sleep(pause);

                            SwingUtilities.invokeAndWait(closerRunner);

                        }

                    catch(Exception e)

                        {

                            e.printStackTrace();

                            // 能夠捕獲InvocationTargetException

                            // 能夠捕獲InterruptedException

                        }

                }

            };

        setVisible(true);

        Thread splashThread = new Thread(waitRunner, "SplashThread");

        splashThread.start();

    }

}

這裡的基本思路是利用一個在一定時間內暫停等待的Thread對象。在上面的代碼中,線程的暫停時間是4秒。當這個線程喚醒時,它將關閉歡迎畫面。由於Swing是非安全執行緒的,除非代碼在事件指派線程上執行,否則它就不應該影響任何UI組件的狀態。所謂事件指派線程,就是Swing中負責繪圖和事件處理的線程。

為瞭解決這個問題,Swing設計者賦予我們安全地把Runnable對象加入UI事件隊列的能力。在本例中,我們用可運行對象closerRunner完成最關鍵的工作。我們把可運行對象傳入SwingUtilities.invokeAndWait()靜態方法,然後SwingUtilities.invokeAndWait()進行所有未完成的UI操作,並執行傳遞給該方法的可運行對象closerRunner的run方法。通過運用一個獨立的線程負責歡迎畫面的關閉操作,應用擔負起了顯示和關閉歡迎畫面之間的所有操作。

如果要讓歡迎畫面總是顯示且使用者不能關閉它,你必須刪除那些隱藏歡迎畫面的代碼。如果要讓歡迎畫面只能由使用者手工關閉,你可以象使用任何其他JWindow對象一樣調用SplashWindow3對象上的setVisible(false)和dispose()方法。

總而言之,藉助於SwingUtilities.invokeAndWait()方法,我們可以安全地建立出多線程歡迎畫面。具體實現時,歡迎畫面可以由使用者點擊關閉,也可以在一定的時間之後自動關閉。Swing所支援的執行緒模式使得應用在顯示歡迎畫面之後仍能夠響應其他事件和處理其他任務。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.