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