简体   繁体   中英

Java Splash screen

I'm trying to make to make some text appear before my applet loads so I've made a simple SSCCE(.org):

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

    public class test extends JApplet {
      public void init() {

                this.add(new JLabel("Button 1"));
                System.out.println("Hello world...");


                try {
                Thread.sleep(3000);
                }catch(Exception hapa) { hapa.printStackTrace(); }


      }
    }

If you run it, the Button 1 will appear AFTER the 3 seconds when it's suppose to appear BEFORE that... what am I doing wrong?

我认为init()方法必须在呈现项目之前返回。

JustinKSU covered the technical part of the question.

A better strategy would be to use the image param to show a 'splash' before the applet appears. See Special Attributes of Applets for further details.

I want one for a fixed amount of time... not just the loading.

In that case, put a CardLayout in the applet. Add the 'splash' to the first card, the rest of the GUI to another. At the end of the init() create a non-repeating Swing Timer that will flip to the card with the main GUI.

EG

// <applet code='SplashApplet' width='400' height='400'></applet>
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SplashApplet extends JApplet {

    public void init() {
        final CardLayout cardLayout = new CardLayout();
        final JPanel gui = new JPanel(cardLayout);

        JPanel splash = new JPanel();
        splash.setBackground(Color.RED);
        gui.add(splash, "splash");

        JPanel mainGui = new JPanel();
        mainGui.setBackground(Color.GREEN);
        gui.add(mainGui, "main");

        ActionListener listener = new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                cardLayout.show(gui, "main");
            }
        };

        Timer timer = new Timer(3000, listener);
        // only needs to be done once
        timer.setRepeats(false);
        setContentPane(gui);
        validate();
        timer.start();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM