2013-04-29 79 views

回答

7

在加載SwingWorker中的東西時,可以使用帶有背景圖像和進度條的未修飾對話框。完成後,隱藏對話框並像往常一樣啓動UI。添加到dialog/splashcreen的組件必須是非透明的,才能「看見」背景圖像。

這裏是一個工作示例:

enter image description here

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Frame; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.List; 

import javax.swing.BorderFactory; 
import javax.swing.ImageIcon; 
import javax.swing.JDialog; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JProgressBar; 
import javax.swing.SwingUtilities; 
import javax.swing.SwingWorker; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class TestSplashScreen { 

    private JDialog dialog; 
    private JFrame frame; 
    private JProgressBar progress; 

    protected void initUI() throws MalformedURLException { 
     showSplashScreen(); 
     SwingWorker<Void, Integer> worker = new SwingWorker<Void, Integer>() { 

      @Override 
      protected Void doInBackground() throws Exception { 
       for (int i = 0; i < 100; i++) { 
        Thread.sleep(100);// Simulate loading 
        publish(i);// Notify progress 
       } 
       return null; 
      } 

      @Override 
      protected void process(List<Integer> chunks) { 
       progress.setValue(chunks.get(chunks.size() - 1)); 
      } 

      @Override 
      protected void done() { 
       showFrame(); 
       hideSplashScreen(); 
      } 

     }; 
     worker.execute(); 
    } 

    protected void hideSplashScreen() { 
     dialog.setVisible(false); 
     dialog.dispose(); 
    } 

    protected void showSplashScreen() throws MalformedURLException { 
     dialog = new JDialog((Frame) null); 
     dialog.setModal(false); 
     dialog.setUndecorated(true); 
     JLabel background = new JLabel(new ImageIcon(new URL("http://blogs.dirteam.com/photos/sanderberkouwer/images/2157/original.aspx"))); 
     background.setLayout(new BorderLayout()); 
     dialog.add(background); 
     JLabel text = new JLabel("Loading, please wait..."); 
     text.setForeground(Color.WHITE); 
     text.setBorder(BorderFactory.createEmptyBorder(100, 50, 100, 50)); 
     background.add(text); 
     progress = new JProgressBar(); 
     background.add(progress, BorderLayout.SOUTH); 
     dialog.pack(); 
     dialog.setLocationRelativeTo(null); 
     dialog.setVisible(true); 
    } 

    protected void showFrame() { 
     frame = new JFrame(TestSplashScreen.class.getSimpleName()); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     JLabel ui = new JLabel("UI loaded and ready"); 
     ui.setBorder(BorderFactory.createEmptyBorder(300, 300, 300, 300)); 
     frame.add(ui); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, 
      UnsupportedLookAndFeelException { 
     UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       try { 
        new TestSplashScreen().initUI(); 
       } catch (MalformedURLException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 
} 
+0

太好了,謝謝。因此,未修飾的對話框是自定義閃屏的唯一選項? – 2013-04-30 06:50:49

+0

@DanielRuf你也可以使用'JWindow'而不是'JDialog',但它看起來是一樣的(它可以節省2行代碼)。有沒有其他的選擇?_也許,但到目前爲止,我想出了上述的一個,這看起來很不錯。 – 2013-04-30 07:03:37

4

一種替代方法是使用SplashScreen API。詳情請參閱How to Create a Splash Screen

我需要以編程方式創建一個閃屏和文本添加到它(和修改)。

請致電SplashScreen.createGraphics()並根據需要塗漆。

+0

標記爲解決方案的答案在JDialog中效果更好。但都工作;-)另外你推薦的解決方案需要一個splashscreen設置爲控制檯參數或清單文件(似乎是這樣)。 – 2013-05-01 18:31:14

相關問題