Переключение между Scene
На первом окне JavaFX имеется ProgressBar. Через Timeline происходит его обновление. Когда прогресс дойдёт до 100% должен сработать ActionEvent, который, в свою очередь, должен закрыть LoadScreen и открыть MainScreen. Проблема заключается в том, что как бы я ни старался, закрыть LoadScreen после открытия MainScreen у меня не выходит. Пытался через :
Parent, Node, stage.hide() и последний вариант переписать первый scene другим.

Launcher.java
package com.fx.javafx;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import java.io.IOException;
public class Launcher extends Application {
Stage mainStage = new Stage();
@Override
public void start(Stage mainStage) throws IOException {
mainStage.setResizable(false);
mainStage.initStyle(StageStyle.UNDECORATED);
this.mainStage = mainStage;
setLoadScene();
}
public void setLoadScene() throws IOException {
AnchorPane loadSceneFXML = FXMLLoader.load(getClass().getResource("/fxml/LoadScreen.fxml"));
Scene loadScene = new Scene(loadSceneFXML);
mainStage.setScene(loadScene);
mainStage.show();
}
public void setMainScene() throws IOException {
AnchorPane mainFXML = FXMLLoader.load(getClass().getResource("/fxml/MainScreen.fxml"));
Scene mainScene = new Scene(mainFXML);
mainStage.setScene(mainScene);
mainStage.show();
}
public static void main(String[] args) {
launch();
}
}
LoadScreen.java(Controller)
package com.fx.javafx.Controllers;
import com.fx.javafx.Launcher;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ProgressBar;
import javafx.util.Duration;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
public class LoadScreen implements Initializable {
Launcher launcher = new Launcher();
@FXML
ProgressBar progressBar = new ProgressBar();
@Override
public void initialize(URL location, ResourceBundle resources) {
Timeline timeline = new Timeline();
KeyValue keyValue = new KeyValue(progressBar.progressProperty(), 1);
EventHandler<ActionEvent> onFinished = event -> {
try {
launcher.setMainScene();
} catch (IOException e) {
throw new RuntimeException(e);
}
};
KeyFrame keyFrame = new KeyFrame(new Duration(3000), onFinished, keyValue);
timeline.getKeyFrames().add(keyFrame);
timeline.play();
progressBar.setProgress(0.0);
}
}
LoadScreen.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.ProgressBar?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.fx.javafx.Controllers.LoadScreen">
<children>
<ProgressBar fx:id="progressBar" layoutX="53.0" layoutY="164.0" prefHeight="71.0" prefWidth="494.0" progress="0.0" />
</children>
</AnchorPane>
MainScreen.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.fx.javafx.Controllers.MainScreen">
<children>
<Label layoutX="48.0" layoutY="136.0" prefHeight="94.0" prefWidth="532.0" text="This is second screen">
<font>
<Font size="49.0" />
</font>
</Label>
</children>
</AnchorPane>