![]() ![]() ![]() |
13
Splash Screen
Features of JCSplashScreen
Classes and Interfaces
Methods and Constructors
Examples
13.1 Features of JCSplashScreen
A splash screen is an image that appears while an application is loading. It serves both as an indication that the program is being loaded from disk and as a place to put notices, such as copyrights, version or release numbers, and the like.
JCSplashScreen
does the following:
- Creates a splash screen given an
Icon
or the location of the image. The image location is the package path of the image and must be in the classpath. AnyIcon
, such as a GIF, JPEG, or other supported image may be used, so long as the time it takes to load is acceptable. An example of an image is in /demos/elements/gauge/gauge.gif.- Once instantiated, a
JCSplashScreen
appears only once. Hiding it causes it to be disposed.
13.2 Classes and Interfaces
The stand-alone class
com.klg.jclass.swing.JCSplashScreen
subclasses fromjava.lang.Object
, providing an independent mechanism for displaying an image in a window in the middle of the screen.No interfaces are used in
JCSplashScreen
.
13.3 Methods and Constructors
Constructors
Methods
13.4 Examples
If you compile and run the code for this example, which is given below, you'll see a message printed on the console informing you that the application has started. The image for the splash screen is loaded and is made visible, then the program enters a wait state until a sleep command times out. An application that takes a long time to load would exhibit similar behavior. The user knows that loading is in progress because the splash screen is visible. It contains whatever graphic information you think is appropriate.
The example uses a
JCExitFrame
to hold a button that controls the disposal of the splash screen. All that is required for its disposal is the commandsetVisible(false)
, but once it is given, the splash screen is gone for good. You would issue this command after receiving notification that the application is ready to run.
Figure 43 : The visible elements in SplashScreenExample.
import com.klg.jclass.swing.*;
import com.klg.jclass.util.swing.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.net.*;
public class SplashScreenExample extends JFrame implements ActionListener {
static JCSplashScreen ss;
static Icon image;
SplashScreenExample() {
URL url = getClass().getResource("/images/SplashScreen.gif");
// convert URL to Image icon
image = new ImageIcon(url);
System.out.println("\n\nLoading an image using URL\n\nURL: " + url);
System.out.println("This example simulates the loading");
System.out.println("of a large application using a sleep command.");
// initialize(image);
ss = new JCSplashScreen(image);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof JButton) {
ss.setVisible(false);
}
}
public static void main(String[] args){
SplashScreenExample sse = new SplashScreenExample();
String title = "Splash Screen Appears while Application Loads";
JCExitFrame frame;
frame = new JCExitFrame(title);
ss = new JCSplashScreen(image);
ss.setVisible(true);
try {
Thread.currentThread().sleep(10000);
} catch (Exception e) {}
Container cp = frame.getContentPane();
JButton btn = new JButton("Click Me and I'll Close the Splash Screen!");
btn.addActionListener(sse);
cp.add(btn);
frame.setSize(450, 100);
frame.setVisible(true);
frame.setExitOnClose(true); // Close the window so the application can exit.
}
}
![]() ![]() ![]() |