Designing a splash screen (java)

2019-01-09 04:25发布

问题:

I want to design a splash screen that can show the current loading process with a progress bar, much like netbeans startup screen, as it shows

loading... modules, done!.... loading modules and so on 

and after the loading finished the main application comes up.

I have read many articles that are related to only creating a splash screen but none of them addresses about How to display progress of different background tasks on a splash screen.

How can I achieve this?

Can I use javafx 2 for splash screen while the rest of the application is written using java

Solved!

I finally managed it to work. My mistake was I was updating the GUI content in the Task Thread so My Task Thread was Blocked and could not execute the next instructions after the GUI update instructions.Now I shifted those Updating GUI instruction after Task Completion, and its working..... Thanks Jewelsea for the right path.

回答1:

I created a splash page sample for a standalone JavaFX 2.0 application previously.

I updated the sample to demonstrate monitoring of the load progress via a progress bar and progress text on the splash page.

To adapt the code to monitor the initialization progress of your application, rather than tying the ProgressBar to a WebEngine's loadWorker.workDone property, create a JavaFX Task which performs expensive initialization work, and monitor the progress of the Task via the Task's progressProperty and messageProperty.

Here is a link to a splash based sample based upon the Task approach outlined in the prior paragraph.

For a WebStart or browser embedded JavaFX application, use a preloader as suggested by assylias.



回答2:

If you use java-web-start for deployment, the icon element can refer to any of a variety of images. Progress indication is handled by the jnlp client during download. The advantage is intervening very early in the user experience.

Later, a SwingWorker can load image(s) in the background, as shown here. The worker's progress() method can drive a JProgressBar via a PropertyChangeListener, as shown here.



回答3:

Using a preloader to display the progress of the application initialisation seems to be what you are looking for.



回答4:

Take a look at the Swing splash-screen tutorial, which contains a sample where a progress bar is updated on the splash screen. You could easily adjust the sample to update some text instead.



回答5:

For completeness, if you want to show a progress bar. Quoting the SplashScreen javadoc:

The splash screen window is closed automatically as soon as the first window is displayed by Swing/AWT.

which means it cannot use standard AWT/Swing components (such as JProgressBar) but can only be drawn through the Graphics2D methods (e.g. drawText(), fillRect(), ...).

So you'd have to emulate a progress bar in a separate class (code is given as a very rough demonstration. You should check for SplashScreen.isVisible()):

public class SplashProgressBar {

    SplashScreen splash;
    Graphics2D g;
    int x, y, w, h; // Drawing position in splash screen

    int val, max; // Progress bar value
    String text; // Text to display

    public SplashProgressBar(SplashScreen splash, int x, int y, int w, int h) {
        this.splash = splash;
        g = splash.createGraphics();
        this.x = x;
        // this.y = y; etc
        this.val = 0;
        this.max = 100;
    }

    public void setMaximum(int n) {
        max = n;
        update();
    }

    public void setValue(int n) {
        val = n;
        update();
    }

    public void setString(String text) {
        this.text = text;
        update();
    }

    public void update() {
        g.setColor(Color.WHITE);
        g.fillRect(x, y, w, h);
        g.setColor(Color.DARK_GRAY);
        g.drawRect(x, y, w, h);
        g.setColor(Color.ORANGE);
        g.fill3DRect(x, y, w * val / max, h, true);
        if (text != null) {
            g.setColor(Color.BLACK);
            g.drawString(text, x + (w - g.getFontMetrics().stringWidth(text)) / 2.0f, y + 8 - 2); // Use TextLayout to get precise text dimensions...
        }
        splash.update();
    }
}

Then:

// Drawn a scroll bar at (10;100) x (300x15) on the splash screen image (we know its size and where it would fit)
SplashProgressBar spb = new SplashProgressBar(SplashScreen.getSplashScreen(), 10, 100, 300, 15);
spb.setMaximum(processing.size());
for (int i = 0; i < processing.size(); i++) {
    spb.setValue(i);
    spb.setString("Processing #"+(i+1));
    // Do the processing...
}


回答6:

You also Create Splash Screen With java,

but if you want to code less and do more than use JavaFx Splash screen Component.