Create a Splash Screen with Window

in

Creating Splash Screen (Welcome or Loading screen) from java window.

File- BaseFrame.java

package goodmorning;
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
 
public class BaseFrame extends JFrame {
  private JPanel contentPane;
  private BorderLayout borderLayout1 = new BorderLayout();
  GoodMorning good =new GoodMorning(this);
 
  //Construct the frame
  public BaseFrame() {
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try {
      jbInit();
      good.setSize(400,300);
      good.setLocation(250,230);
      good.setVisible(true);
        try{
          Thread.sleep(3000);
          good.setVisible(false);
           }catch(InterruptedException ex){
            System.out.println(""+ex.toString());
           }
 
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  //Component initialization
  private void jbInit() throws Exception  {
    //setIconImage(Toolkit.getDefaultToolkit().createImage(Frame.class.getResource("[Your Icon]")));
    contentPane = (JPanel) this.getContentPane();
    contentPane.setLayout(borderLayout1);
    this.setSize(new Dimension(400, 300));
    this.setTitle("Testing Good Morning window");
  }
  //Overridden so we can exit when window is closed
  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
      System.exit(0);
    }
  }
}
class GoodMorning extends Window {
    Label label;
     Toolkit to=Toolkit.getDefaultToolkit();
     Image im=to.getImage("key.jpg");
    GoodMorning(BaseFrame af){
        super(af);
        setLayout(new FlowLayout());
        //Font f=new Font("",4,15)
        label = new Label("Created by Ishara !");
        label.setFont(new java.awt.Font("Dialog", 3, 16));
        add(label);
 
 
    }
 
 
    public void paint (Graphics g)
    {
        int width = getSize().width;
        int height = getSize().height;
 
        g.drawRect(0, 0, --width, --height);
        g.drawImage(im,width/6,height/5,this);
    }
}

File - Application.java

package goodmorning;
 
import javax.swing.UIManager;
import java.awt.*;
 
 
public class Application {
  private boolean packFrame = false;
 
  //Construct the application
  public Application() {
    BaseFrame frame = new BaseFrame();
    //Validate frames that have preset sizes
    //Pack frames that have useful preferred size info, e.g. from their layout
    if (packFrame) {
      frame.pack();
    }
    else {
      frame.validate();
    }
    //Center the window
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = frame.getSize();
    if (frameSize.height > screenSize.height) {
      frameSize.height = screenSize.height;
    }
    if (frameSize.width > screenSize.width) {
      frameSize.width = screenSize.width;
    }
    frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
    frame.setVisible(true);
  }
  //Main method
  public static void main(String[] args) {
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }
    catch(Exception e) {
      e.printStackTrace();
    }
 
    new Application();
  }
 
}

Splash Window
Main App
AttachmentSize
splash_IDEA_Project.zip17.61 KB