adsense

Hii, welcome to my site. My name is Om prakash kartik. This blog helps you to learn programming languages concepts.

Create a Splash Screen in JAVA

Create a Splash Screen in JAVA 

import java.awt.Font;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import java.awt.Color;

public class SplashScreen extends JWindow{

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;

	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setBounds(200,  100, 400, 400);
		
//		 create object of SplashScreen
		SplashScreen sp = new SplashScreen(frame, "Splash screen Demo");
		
	}

	public SplashScreen(JFrame frame, String msg) {
//		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 513, 331);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel(msg);
		lblNewLabel.setForeground(new Color(255, 69, 0));
		lblNewLabel.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 18));
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel.setBounds(90, 140, 298, 38);
		contentPane.add(lblNewLabel);
		
		setLocationRelativeTo(null);
		setAlwaysOnTop(true);
		setVisible(true);
		
		// Splash screen 
		int width = 500, height = 300;
				
		// Total width and height of window 
		//outerHeight: 744
//		outerWidth: 1366
		
		int totalWidth = 1366, totalHeight = 744;
		
		this.setSize(width, height);
		this.setLocation((int)(totalWidth - width) / 2, (int)(totalHeight - height) / 2);

//		Timer 
		Timer timer = new Timer();
		timer.schedule(new TimerTask() {
			public void run() {
				frame.setVisible(true);
				// Splash Screen  close
				dispose();
//				dispose(); or SplashScreen.this.dispose();
			}
		}, 5000); // 5 seconds
		
	}

}

Pictures






YouTube Video






Post a Comment

0 Comments