初始化后的JavaFX FadeTransition通过控制器完成

时间:2018-05-09 06:03:12

标签: javafx fade transitions

我有这个应用程序:

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已经看到。

1 个答案:

答案 0 :(得分:0)

试试这个:

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类

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