通过控制器完成初始化后的 JavaFX 淡入淡出过渡

JavaFX FadeTransition in after init is finished via controler

我有这个应用程序:

public class FOO extends Application {

    private Scene scene;

    @Override
    public void start(Stage stage) throws Exception {
        stage.initStyle(StageStyle.UNDECORATED);
        stage.setScene(scene);
        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void init() throws Exception {

        URL resource = FXMLTabPaneController.class.getResource("FXMLTabPane.fxml");
        System.out.println(resource);
        Parent root = FXMLLoader.load(resource);

        scene = new Scene(root);
    }

}

作为控制器:

public class FXMLTabPaneController implements Initializable {

@FXML
private TabPane tabPane;

@FXML
private AnchorPane anchorPane;

@Override
public void initialize(URL url, ResourceBundle rb) {
    FadeTransition fadein = new FadeTransition(Duration.seconds(5), tabPane);
    fadein.setFromValue(0);
    fadein.setToValue(1);
    fadein.play();

}

}

我想在应用程序启动后慢慢显示 Tabpane,但它从已经看到的应用程序 Tabpane 开始。

试试这个:

public class FXMLTabPaneController {

    @FXML
    private TabPane tabPane;

    @FXML
    private AnchorPane anchorPane;

    @FXML
    private void initialize() {

    }

    public void show(WindowEvent event) {
        FadeTransition fadein = new FadeTransition(Duration.seconds(5), tabPane);
        fadein.setFromValue(0);
        fadein.setToValue(1);
        fadein.play();
    }
}

和 FOO class

public class FOO extends Application {

    private Scene scene;
    private FXMLTabPaneController controller;

    @Override
    public void init() throws Exception {
        super.init();
        URL resource = FXMLTabPaneController.class.getResource("FXMLTabPane.fxml");
        System.out.println(resource);

        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(resource);

        Parent root = loader.load();
        controller = loader.getController();

        scene = new Scene(root);
    }

    @Override
    public void start(Stage stage) throws Exception {
        stage.setOnShown(controller::show);

        stage.initStyle(StageStyle.UNDECORATED);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}