如何实现启动画面Java

时间:2013-03-31 02:38:31

标签: java multithreading swing splash-screen

我得到了这个用于在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();
    }
}

我尝试像这样实现:     ...     public static void main(String [] args){             JFrame frame = new JFrame();             frame.setSize(500,500);

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

但没有任何表现。 请帮帮我,谢谢:)

1 个答案:

答案 0 :(得分:1)

不要说:

"setVisible(true);"

在构造函数中,

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

写:

window.setVisible(true);