-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);
    }
...

但没有什么可显示的。请帮助我,谢谢:)

4

1 回答 1

1

不要放:

"setVisible(true);"

在构造函数中,之后

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

写:

window.setVisible(true);
于 2013-03-31T08:26:18.360 回答