Welcome screen with Java Swing

xiaoxiao2021-03-06  63

Source: CN-Java almost all fashionable applications have a welcome screen. Welcome screen is one of the methods of promotional products, and during long-term application startup, the welcome screen is also used to indicate that the application is in the process of preparation.

Here is the simplest welcome screen implementation:

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 is derived from Swing JWindow. JWindow is a container, which does not have a variety of window elements having other windows, such as title strips, window management buttons, and even highlighting borders. Therefore, Jwindow is very suitable for making the Welcome Screen. The above code assumes a graphic file in the current directory. The graph is loaded into memory via ImageICON, and then it is placed in the center of JWindow. Next, the window is pividual (), which makes swing to adjust the window to the appropriate size, and the final window is moved to the center of the screen.

If we run the above program, you can find that although the welcome picture does appear on the center of the screen, it is unfortunate, 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);}}

Compared with the original SplashWindow1 class, the only difference between this SplashWindow2 class lies in an anonymous mouselistener installed on jwindow. After this change, the user can click on the welcome screen to close it.

Now we have a very good welcome screen, it can be turned off by clicking on, but it will not disappear. Next we have to join the code, so that the welcome screen will automatically disappear after displaying a certain time. Here we have to consider using threads. class SplashWindow3 extends JWindow {public SplashWindow3 (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 (); // Capture InvocationTargeTexception // Capture InterruptedException}}}}}}}} ISIBLE (TRUE); Thread Splashthread = New Thread (Waitrunner, "Splashthteread"); splashthread.start ();}}

The basic idea here is to use a THREAD object that is waiting for a certain period of time. In the above code, the temporary time of the thread is 4 seconds. When this thread wakes up, it will close the welcome screen. Since Swing is non-threaded secure, unless the code is executed on the event distribution thread, it should not affect the status of any UI components. The so-called event assignment thread is the thread responsible for drawing and event processing in Swing.

In order to solve this problem, Swing designers give us the ability to secure the runnable object to the UI event queue. In this case, we use the Object Closerrunner to complete the most critical work. We have passed the runofitIlities.invokeAndWait () static method, then Wingutilities.InvokeAndWait () performs all unfinished UI operations, and performs RUN methods that passed to the Runable Object CloserRunner that passes the method. By using a separate thread, the application is responsible for the shutdown operation of the screen, and the application is responsible for all the operations between the welcome screen.

If you want the welcome screen to always display and the user can't close it, you must delete the code that hides the welcome screen. If you want to make the welcome screen can only be manually shut down by the user, you can call the setvisible (false) and Dispose () methods on the SplashWindow3 object like any other JWindow object.