Add a Splash screen to your Java desktop application __java

Source: Internet
Author: User
Tags java se

Copyright NOTICE: Please be sure to retain the following author information and links when reprint
Author: Chen Wei Xintian (chenweionline@hotmail.com) Author's website: Http://www.chenwei.mobi

When the application is started, the initialization and loading of various class libraries occur, and a striking Splash screen prompts the user that the software is initializing and can give the software user more positive feedback by displaying ads, legal licenses, and software logos, among other things.

Friends who are familiar with Java SE 6 should know that Java 6 allows an application to display a Splash screen before the JVM is started, and what I'm going to say today is for countless users using older versions of Java-How to create an SPL in software developed prior to Java 6 Ash screen.

We can think of the Splash screen as a canvas component that appears before the main form of the program, and when the program is initialized, the main form is displayed on the screen and the Splash screen closes automatically, fulfilling its mission.

After understanding the behavior and function of the Splash screen, I chose to extend the jdialog to accomplish it.

first customize the appearance of the jdialog so that it conforms to the Splash screen shape.

alwaysontop The form to a top-level form by setting the property in the constructor always appears at the top of the screen; property undecorated Disables dialog decoration, which removes the text bar on the form and the maximum/minimized form /Close button so that it is fully present as a canvas, and then the Size property is set to appear in the middle of the screen based on the sizing size on which the image is drawn and the Locationrelativeto property is set to null. Setalwaysontop (TRUE);
Setundecorated (TRUE);
SetSize (New Dimension (Image.getwidth (), Image.getheight ()));
Setlocationrelativeto (NULL);

The paint (Graphics g) Method of dialog is then overridden to complete the drawing on the canvas of the specified image (advertising, legal license, logos, and so on). public void Paint (Graphics g) ... {
if (image!= null) ... {
G.drawimage (image, 0, 0, this);
}
}

Next we need to write code that controls Splash behavior.

From the user experience, the Splash screen appears before the program main form is opened, and closes when the main form is opened. Then it must be associated with the Open event of the main form, we pass a reference to the main form in the dialog constructor, add a componentlistener to it, and destroy the current dialog instance when the main form becomes visible. Frame.addcomponentlistener (New Componentadapter () ... {

@Override
public void Componentshown (ComponentEvent e) ... {
JSplashScreen.this.dispose ();
}
} );

Here, the main code for a Splash screen is complete.

by adding a mouse listener to it to listen for mouse down events, it can also be used as the about form. When the mouse clicks on the canvas, the form is automatically destroyed. At the same time, we define two static constants in this component to identify whether it is used as Splash or about. public static final int splash_mode = 0;
public static final int about_mode = 1;


if (mode = = Splash_mode) ... {
Frame.addcomponentlistener (New Componentadapter () ... {

@Override
public void Componentshown (ComponentEvent e) ... {
JSplashScreen.this.dispose ();
}
});
else ... {
Addmouselistener (New Mouseadapter () ... {

@Override
public void mouseclicked (Java.awt.event.MouseEvent evt) ... {
Dispose ();
}
});
}

  Below is the complete code for the Splash screen component  jsplashscreen.java /**//*
 * jsplashscreen.java
 *
 * created on 2007 year November 20,  afternoon 1:42
 */
Package   mobi.chenwei.wing.swing;

Import  java.awt.dimension;
import  java.awt.frame;
Import  java.awt.graphics;
Import  java.awt.event.ComponentAdapter;
Import  java.awt.event.ComponentEvent;
Import  java.awt.event.MouseAdapter;

/** *//**
 * splash screen
 * , depending on the  mode  property settings, can also be used as a  about   form use.
 * 
 *  @author  chen wei
 *  @website  www.chenwei.mobi
 *  @email  chenweionline@hotmail.com
 */
Public  

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.