首页 文章

如何实现启动画面Java [重复]

提问于
浏览
-1

这个问题在这里已有答案:

我得到了这个用于在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 回答

  • 1

    不要说:

    "setVisible(true);"
    

    在构造函数之后

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

    写:

    window.setVisible(true);
    

相关问题