简体   繁体   中英

JavaFx ImageView SplashScreen not working

I want to use a .png picture to show as the splash screen through a Preloader and an ImageView, but the .png image is not showing. (The .png image is located in the same directory as the class specified below.)

import java.io.File;

import javafx.application.Preloader;
import javafx.scene.Scene;
import javafx.scene.effect.DropShadow;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class Splash extends Preloader {

    private Stage splashStage;

    @Override
    public void start(Stage primaryStage) throws Exception {
        this.splashStage = primaryStage;
        this.splashStage.setWidth(800);
        this.splashStage.setHeight(300);
        this.splashStage.setResizable(false);
        this.splashStage.initStyle(StageStyle.TRANSPARENT);
        Pane pane = new Pane();
        Scene scene = new Scene(pane, 800, 300);
        ImageView splashImage = new ImageView(new Image(new File("Splash.png").toURI().toString()));
        splashImage.setEffect(new DropShadow(4, Color.GREY));
        pane.getChildren().add(splashImage);
        splashImage.setFitWidth(800);
        splashImage.setFitHeight(800);
        this.splashStage.setScene(scene);
        this.splashStage.show();
        Thread.sleep(3000);
    }

    @Override
    public void handleStateChangeNotification(StateChangeNotification stateChangeNotification) {
        if(stateChangeNotification.getType() == StateChangeNotification.Type.BEFORE_START) {
            splashStage.hide();
        }
    }

    public Stage getSplashScreen() {
        return this.splashStage;
    }

}

Try using StageStyle.UNDECORATED rather than StageStyle.TRANSPARENT . Also, I'm not sure that you have to specify a new file in a new Image to instantiate the ImageView. Try using just the image file name. Like, ImageView splashImage = new ImageView("Splash.png");

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