Use swing to create a welcome screen

Source: Internet
Author: User
Almost all trendy apps have a welcome screen. The welcome screen is not only one of the methods to promote the product, but also indicates that the application is being prepared during a long period of application startup.

The following is a simple screen welcome 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. getdefatooltoolkit (). 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 the jwindow of swing. Jwindow is a container that does not have any other window elements, such as the title bar, window management button, or even the highlighted border. Therefore, jwindow is very suitable for making welcome screens. The above Code assumes that the graphic file is in the current directory. The image is loaded into the memory through imageicon, and then it is placed in the center of jwindow. Then, the window is packed (), which enables swing to adjust the window to the appropriate size, and finally the 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 close 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. getdefatooltoolkit (). 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 is that there is an additional anonymous mouselistener installed on jwindow. After this change, you can click the welcome screen to close it.

Now we have a very good welcome screen, which can be closed by clicking, but it will not disappear by itself. Next we will add code to make the welcome screen disappear automatically after a certain period of time. Here we need to consider the use of 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. getdefatooltoolkit (). 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

}

}

};

Setvisible (true );

Thread splashthread = new thread (waitrunner, "splashthread ");

Splashthread. Start ();

}

}

The basic idea here is to use a thread object to pause waiting for a certain period of time. In the above Code, the thread suspension time is 4 seconds. When this thread wakes up, it will close the welcome screen. Since swing is non-thread-safe, it should not affect the status of any UI component unless the code is executed on the event dispatching thread. The so-called event dispatch thread is the thread responsible for drawing and event processing in swing.

To solve this problem, the swing designer gives us the ability to securely add runnable objects to the UI event queue. In this example, we use the executable object closerrunner to complete the most critical work. We passed the runable object to swingutilities. invokeandwait () static method, and then swingutilities. invokeandwait () performs all unfinished UI operations and executes the run method of the executable object closerrunner passed to the method. By using an independent thread to take charge of the closing operation of the welcome screen, the application undertakes all the operations between displaying and closing the welcome screen.

To make the welcome screen always show and the user cannot close it, you must delete the code that hides the welcome screen. To enable the welcome screen to be disabled manually, you can call the setvisible (false) and dispose () Methods on the splashwindow3 object just like any other jwindow objects.

All in all, with the help of the swingutilities. invokeandwait () method, we can safely create a multi-threaded welcome screen. In specific implementation, the welcome screen can be closed by a user click or automatically closed after a certain period of time. The thread model supported by swing enables applications to respond to other events and process other tasks after the welcome screen is displayed.

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.