How to make a splash screen for GUI?

纵饮孤独 提交于 2019-11-28 00:12:03
Alya'a Gamal

Simplest one , is to create JFrame and add your screen on it then use Thread.Sleep(long millies)

Try this code:

JWindow window = new JWindow();
window.getContentPane().add(
    new JLabel("", new ImageIcon(new URL("http://docs.oracle.com/javase/tutorial/uiswing/examples/misc/SplashDemoProject/src/misc/images/splash.gif")), SwingConstants.CENTER));
window.setBounds(500, 150, 300, 200);
window.setVisible(true);
try {
    Thread.sleep(5000);
} catch (InterruptedException e) {
    e.printStackTrace();
}
window.setVisible(false);
JFrame frame = new JFrame();
frame.add(new JLabel("Welcome"));
frame.setVisible(true);
frame.setSize(300,100);
window.dispose();

Or you can Create a Splash Screen by using SplashScreen class

See also How to Create a Splash Screen for the AWT based splash functionality.

There's a pretty descent beginners tutorial here which explains how to create such a screen, with a step by step guide how to get there. Wouldn't hurt to Google first, I'd say.

This works fine for me.The functions such as getScreenSize() ,getWidth() and getHeight() can be replaced by own values.

public class splash extends JWindow 
{
  public splash()
  {
     JWindow j=new JWindow();

     Dimension d=Toolkit.getDefaultToolkit().getScreenSize();

     Icon img= new ImageIcon(this.getClass().getResource("2.jpg"));
     JLabel label = new JLabel(img);
     label.setSize(200,300);
     j.getContentPane().add(label);
     j.setBounds(((int)d.getWidth()-722)/2,((int)d.getHeight()-401)/2,722,401);
     j.setVisible(true);
     try
     {
        Thread.sleep(6000);
     }
    catch(InterruptedException e)
    {
        e.printStackTrace();
    }
     j.setVisible(false);

  }
  public static void main(String[] args)
  {
    splash s=new splash();
  }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!