JavaFx ImageView SplashScreen无法正常工作

时间:2015-11-22 22:42:45

标签: java image javafx-2 png

我想使用.png图片通过Preloader和ImageView显示为启动画面,但是.png图像没有显示。 (.png图像与下面指定的类位于同一目录中。)

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;
    }

}

1 个答案:

答案 0 :(得分:0)

尝试使用StageStyle.UNDECORATED而不是StageStyle.TRANSPARENT。此外,我不确定您是否必须在新图像中指定一个新文件来实例化ImageView。尝试仅使用图像文件名。比如,ImageView splashImage = new ImageView("Splash.png");