How to Create a Splash Screen in Java

Language

This is a tutorial in which we will going to create a program that will have a Splash Screen in Java. So, now let's start this tutorial! 1. Open JCreator or NetBeans and make a java program with a file name of splashScreen.java. 2. Import the following packages:
  1. import java.awt.*; //used to access the Color, Dimension, and Toolkit class
  2. import javax.swing.*; //use the JWindow and JLabel class
3. Initialize your variable in your Main. The variable window for JWindow because we will use a window to hold other components as it will have no border and title.
  1. JWindow window = new JWindow();
We will make an Integer variable named duration that will have the value of 5000. This 5000 will be equal to 5 seconds. This will trigger the window to display in 5 seconds. You can also change the value of it of whatever you like.
  1. int duration = 5000;
We will create variables for the screen width and height. We will named it as width and height integer variable.
  1. int width = 450;
  2. int height = 115;
Next, we will get the screen size of our computer. We will use the Dimension class and named it as variable screen. Then we will create another variable the x and y as Integer to get the center location of the screen. Have this code below:
  1. int x = (screen.width - width) / 2;
  2. int y = (screen.height - height) / 2;
4. We can set the size and location of our window using the setBounds method.
  1. window.setBounds(x, y, width, height);
Add a label on the window using the add method and have it in Center position of the default BorderLayout as layout manager.
  1. window.getContentPane().add(new JLabel("This is a Splash Screen"),"Center");
To set the background color of the JWindow, we will have the setBackground method. And set its visibility to true as we will use the setVisible method.
  1. window.getContentPane().setBackground(Color.CYAN);
  2. window.setVisible(true);
5. We will create then a try and catch method. In the try, we will be using a Thread and its sleep method and put inside variable duration on it. This will trigger the screen to be faded out in 5 seconds.
  1. try {
  2. Thread.sleep(duration);
  3. } catch (Exception e) {
  4. }
After executing the try and catch method, we will make use of the setVisible method and will be equal to false to hide the window after 5 seconds (this is because of the duration=5000) of displaying it.
  1. window.setVisible(false);
Output: output Here's the full code of this tutorial:
  1. import java.awt.*; //used to access the Color, Dimension, and Toolkit class
  2. import javax.swing.*; //use the JWindow and JLabel class
  3. public class splashScreen {
  4.  
  5. public static void main(String[] args) {
  6.  
  7.  
  8. JWindow window = new JWindow();
  9. int duration = 5000;
  10. int width = 450;
  11. int height = 115;
  12.  
  13. Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
  14.  
  15. int x = (screen.width - width) / 2;
  16. int y = (screen.height - height) / 2;
  17.  
  18. window.setBounds(x, y, width, height);
  19. window.getContentPane().add(new JLabel("This is a Splash Screen"),"Center");
  20. window.getContentPane().setBackground(Color.CYAN);
  21. window.setVisible(true);
  22. try {
  23. Thread.sleep(duration);
  24. } catch (Exception e) {
  25. }
  26. window.setVisible(false);
  27. }
  28. }
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment