我尝试在我的JavaFX应用程序中添加启动画面。显示的启动画面框架,但内部图像不加载。我可以通过调试器看到,在视图中设置图像集,但它不可见。这是源代码。
应用程序启动器
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
LOGGER.info("Start Application Frame");
LOGGER.debug("Open splash screen");
Parent splashScreen = showSplashScreen(primaryStage);
Platform.runLater(() -> {
ApplicationContext springContext = InitSpring.init();
LOGGER.debug("Finish init spring - Start my application");
springContext.getBean(ChangeViewService.class).changeView(splashScreen, springContext.getBean(MenuScreenController.class).getView());
});
}
private Parent showSplashScreen(Stage primaryStage) {
Pane root = null;
try {
root = FXMLLoader.load(getClass().getResource("/views/spalshscreen.fxml"));
} catch (IOException e) {
LOGGER.error("Fail to load splash screen", e);
return null;
}
Scene scene = new Scene(root);
// It comes later
//scene.setFill(Color.TRANSPARENT);
//primaryStage.initStyle(StageStyle.TRANSPARENT);
primaryStage.setScene(scene);
primaryStage.setHeight(ApplicationSettings.SPLASH_SCREEN_HEIGHT);
primaryStage.setWidth(ApplicationSettings.SPLASH_SCREEN_WIDTH);
primaryStage.show();
return root;
}
启动屏幕视图
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<Pane maxHeight="400" maxWidth="600" minHeight="400" minWidth="600" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="SplashScreenController">
<children>
<ImageView fx:id="splashImage" fitHeight="400.0" fitWidth="600.0" pickOnBounds="true" preserveRatio="true" />
</children>
</Pane>
SplashScreenController
import de.uni_bremen.roborally.gui.ApplicationSettings;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import org.apache.log4j.Logger;
import java.net.URL;
import java.util.ResourceBundle;
public class SplashScreenController implements Initializable {
private static final Logger LOGGER = Logger.getLogger(SplashScreenController.class);
@FXML private ImageView splashImage;
@Override
public void initialize(URL location, ResourceBundle resources) {
Image image = new Image(getClass().getResourceAsStream("/game/splash.png"));
LOGGER.debug("Is loaded: " + image.isError());
splashImage.setImage(image);
splashImage.setFitHeight(ApplicationSettings.SPLASH_SCREEN_HEIGHT);
splashImage.setFitWidth(ApplicationSettings.SPLASH_SCREEN_WIDTH);
}
}
初始化方法触发正确,但图像不显示。