2013-03-31 51 views
-1

我得到了用Java创建启动画面和线程管理的源代码。但我不知道如何实现它。如何实现启动画面Java

public class SplashWindow extends JWindow { 
    public SplashWindow(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(); 
          // can catch InvocationTargetException 
          // can catch InterruptedException 
         } 
       } 
      }; 
     setVisible(true); 
     Thread splashThread = new Thread(waitRunner, "SplashThread"); 
     splashThread.start(); 
    } 
} 

我尝试实施这样的: ... 公共静态无效的主要(字串[] args){ 的JFrame帧=新的JFrame(); frame.setSize(500,500);

 SplashWindow window = new SplashWindow("splash-scren.jpg", frame, 1000); 
    } 
... 

但没有显示。 请帮帮我,谢谢:)

+5

不要推倒重来; Java已经有了[启动画面]的内置实现(http://docs.oracle.com/javase/tutorial/uiswing/misc/splashscreen.html)。注意:此启动画面在* JVM启动之前显示*。之后,您可以使用['SplashScreen'](http://docs.oracle.com/javase/7/docs/api/java/awt/SplashScreen.html)类更改图像/ etc。 – Jeffrey

+0

另请参阅[* Java Web Start *](http://stackoverflow.com/tags/java-web-start/info)和此[问与答](http://stackoverflow.com/q/11399971/230513)。 。 – trashgod

回答

1

不要放:

"setVisible(true);" 

在构造函数中,

SplashWindow window = new SplashWindow("splash-scren.jpg", frame, 1000); 

写:

window.setVisible(true);