Java Production Welcome Screen 123

Source: Internet
Author: User
Tags final pack thread window
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.





1. 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 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:





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


 }


}








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)


 {


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


//ability to capture InvocationTargetException


//ability to capture interruptedexception


    }


   }


  };


setvisible (TRUE);


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.











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.