Making the Welcome screen with Java Swing

Source: Internet
Author: User
Tags pack

Almost all stylish apps have a welcome screen. The Welcome screen is one of the ways to promote the product, and during the long application startup process, the Welcome screen is also used to indicate that the application is in the process of being prepared.

Here is one of the simplest welcome screen implementations:

class SplashWindow1 extends JWindow
{
  public SplashWindow1(String filename, Frame f)
  {
   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));
   setVisible(true);
   screenSize = null;
   labelSize = null;
  }
}

The SplashWindow1 class derives from the JWindow of swing. JWindow is a container that does not have various window elements, such as title bars, window management buttons, or even highlighted borders, that other windows have. Therefore, JWindow is very suitable for making the Welcome screen. The above code assumes that the graphics file is in the current directory. The graph is loaded into memory by ImageIcon, and then it is placed in the center of the JWindow. Next, the window is Pack (), which allows swing to resize the window to the appropriate size, and the last window is moved to the center of the screen.

If we run the above program, we can find that although the Welcome screen does appear in the center of the screen, but unfortunately, it will not close! To turn off the Welcome screen, we need to add more code:

class SplashWindow2 extends JWindow
{
  public SplashWindow2(String filename, Frame f)
  {
   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();
    }
   });
   setVisible(true);
  }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.