How to Create a Splash Screen in Java
Submitted by donbermoy on Thursday, November 27, 2014 - 18:27.
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:
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.
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.
We will create variables for the screen width and height. We will named it as width and height integer variable.
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:
4. We can set the size and location of our window using the setBounds method.
Add a label on the window using the add method and have it in Center position of the default BorderLayout as layout manager.
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.
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.
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.
Output:
Here's the full code of this tutorial:
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
- import java.awt.*; //used to access the Color, Dimension, and Toolkit class
- import javax.swing.*; //use the JWindow and JLabel class
- int duration = 5000;
- int width = 450;
- int height = 115;
- int x = (screen.width - width) / 2;
- int y = (screen.height - height) / 2;
- window.setBounds(x, y, width, height);
- window.setVisible(true);
- window.setVisible(false);
- import java.awt.*; //used to access the Color, Dimension, and Toolkit class
- import javax.swing.*; //use the JWindow and JLabel class
- public class splashScreen {
- int duration = 5000;
- int width = 450;
- int height = 115;
- int x = (screen.width - width) / 2;
- int y = (screen.height - height) / 2;
- window.setBounds(x, y, width, height);
- window.setVisible(true);
- try {
- }
- window.setVisible(false);
- }
- }
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
- 979 views