We can add a welcome screen at the beginning of the application. 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.
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.
2. 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:
the only difference between the SplashWindow2 class and the original SplashWindow1 class is that there is an extra anonymous mouselistener installed on the JWindow. After this change, the user can click on the Welcome screen to close it.
3. Now we have a nice welcome screen that can be turned off by clicking, but it won't go away on its own. Next we're going to add code to make the Welcome screen disappear automatically after a certain amount of time is displayed. Here we have to consider the use of threads.
class SplashWindow3 extends JWindow
{
public SplashWindow3 (String filename, Frame f, int waittime)
thread splashthread = new Thread (Waitrunner, "Splashthread");
Splashthread.start ();
}
}
The basic idea here is to use a thread object that pauses waiting for a certain amount of time. In the above code, the thread's pause time is 4 seconds. When this thread wakes up, it closes the Welcome screen. Because swing is not thread-safe, it should not affect the state of any UI component unless the code executes on the event dispatch thread. The so-called event dispatch thread is the thread that is responsible for drawing and event handling in swing.
to solve this problem, swing designers give us the ability to safely add Runnable objects to the UI event queue. In this case, we use the Closerrunner object to complete the most critical work. We pass the runtime object to the Swingutilities.invokeandwait () static method, and then wingutilities.invokeandwait () all the unfinished UI operations. and executes the Run method Closerrunner the executable object passed to the method. By using a separate thread to handle the Welcome screen shutdown, the application assumes all the actions between the display and the close Welcome screen.
If you want the welcome screen to always be displayed and the user cannot close it, you must remove the code that hides the Welcome screen. If you want the welcome screen to be closed only by the user, you can invoke the setvisible (false) and Dispose () methods on the SplashWindow3 object as you would any other JWindow object.
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.