11

Hi there I'm new to GUIs in Java and was trying to make a splash screen or an image appear for 3 seconds. Then after that it it will go onto my main program. Does anyone have an ideas how to do this or can link me to any tutorials?

So far I have done this but not sure where to go from here.

public static void main(String[] args)
{
    splashInit();           // initialize splash overlay drawing parameters
    appInit();              // simulate what an application would do 
}
4

4 回答 4

12

Simplest one , is to create JFrame and add your screen on it then use Thread.Sleep(long millies)

Try this code:

JWindow window = new JWindow();
window.getContentPane().add(
    new JLabel("", new ImageIcon(new URL("http://docs.oracle.com/javase/tutorial/uiswing/examples/misc/SplashDemoProject/src/misc/images/splash.gif")), SwingConstants.CENTER));
window.setBounds(500, 150, 300, 200);
window.setVisible(true);
try {
    Thread.sleep(5000);
} catch (InterruptedException e) {
    e.printStackTrace();
}
window.setVisible(false);
JFrame frame = new JFrame();
frame.add(new JLabel("Welcome"));
frame.setVisible(true);
frame.setSize(300,100);
window.dispose();

Or you can Create a Splash Screen by using SplashScreen class

于 2013-04-21T18:49:39.130 回答
11

See also How to Create a Splash Screen for the AWT based splash functionality.

splash image

于 2013-04-22T13:55:11.810 回答
-2

This works fine for me.The functions such as getScreenSize() ,getWidth() and getHeight() can be replaced by own values.

public class splash extends JWindow 
{
  public splash()
  {
     JWindow j=new JWindow();

     Dimension d=Toolkit.getDefaultToolkit().getScreenSize();

     Icon img= new ImageIcon(this.getClass().getResource("2.jpg"));
     JLabel label = new JLabel(img);
     label.setSize(200,300);
     j.getContentPane().add(label);
     j.setBounds(((int)d.getWidth()-722)/2,((int)d.getHeight()-401)/2,722,401);
     j.setVisible(true);
     try
     {
        Thread.sleep(6000);
     }
    catch(InterruptedException e)
    {
        e.printStackTrace();
    }
     j.setVisible(false);

  }
  public static void main(String[] args)
  {
    splash s=new splash();
  }
}
于 2019-08-06T16:46:26.823 回答
-2

I use this code. Maybe you need to change some parts:

import javax.swing.*;
import java.awt.*;

public class SplashScreen {
    private final JWindow window;
    private long startTime;
    private int minimumMilliseconds;

    public SplashScreen() {
        window = new JWindow();
        var image = new ImageIcon("C:\\example.jpg");
        window.getContentPane().add(new JLabel("", image, SwingConstants.CENTER));
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        window.setBounds((int) ((screenSize.getWidth() - image.getIconWidth()) / 2),
                (int) ((screenSize.getHeight() - image.getIconHeight()) / 2),
                image.getIconWidth(), image.getIconHeight());
    }

    public void show(int minimumMilliseconds) {
        this.minimumMilliseconds = minimumMilliseconds;
        window.setVisible(true);
        startTime = System.currentTimeMillis();
    }

    public void hide() {
        long elapsedTime = System.currentTimeMillis() - startTime;
        try {
            Thread.sleep(Math.max(minimumMilliseconds - elapsedTime, 0));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        window.setVisible(false);
    }
}

And here is how to use it:

    var splash = new SplashScreen();
    splash.show(2000);

    // Initializing...

    splash.hide();

This will show the splash at least 2 seconds.

于 2019-09-24T17:18:12.453 回答