在JavaFx中淡入/淡出屏幕

时间:2014-07-27 05:48:30

标签: animation javafx screen fadein fadeout

我创建了一个具有主屏幕的JavaFx应用程序。主屏幕包含按钮,每个按钮显示不同的屏幕。现在我想,当我按下按钮时,我当前的屏幕显示 FadeOut ,新屏幕显示为 FadeIn

public class GuiPractice extends Application {

    private Stage stage;

    public static void main(String[] args) {
        Application.launch(GuiPractice.class, (java.lang.String[])null);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        try{
            stage = primaryStage;
            stage.setTitle("HOME SCREEN");
            gotoWelcome();
            stage.show();            
        } catch(Exception ex){
            Logger.getLogger(GuiPractice.class.getName()).log(Level.SEVERE,null,ex);
        }
    }


    private void gotoWelcome(){
        try{
            WelcomePageController wc = (WelcomePageController)     replaceScene("WelcomePage.fxml");  // Check 'replaceScene' Below
            wc.setApp(this);
        } catch (Exception ex){
            Logger.getLogger(GuiPractice.class.getName()).log(Level.SEVERE,null,ex);
        }
    }
    // When I Press Button Action Handler Calls This Method..
    // This Method is Working Fine, Except For the FandIn Portion
    private void gotoSignin(){
        try{

            SigninPageController wc = (SigninPageController)     replaceScene("SigninPage.fxml"); // Check 'replaceScene' Below
            wc.setApp(this);           

            /// THIS PORTION IS FADE TRANSITION, IT IS NOT WORKING
            FadeTransition ft = new FadeTransition(Duration.millis(3000));
            ft.setFromValue(0.0);
            ft.setToValue(1.0);

            ft.play();


        } catch (Exception ex){
            Logger.getLogger(GuiPractice.class.getName()).log(Level.SEVERE,null,ex);
        }
    }

    private Initializable replaceScene(String fxml) throws Exception {
        FXMLLoader loader = new FXMLLoader();
        InputStream ins = GuiPractice.class.getResourceAsStream(fxml);
        loader.setBuilderFactory(new JavaFXBuilderFactory());
        loader.setLocation(GuiPractice.class.getResource(fxml));
        AnchorPane page;
        try {
            page = (AnchorPane) loader.load(ins);
        } finally {
            ins.close();
        }
        Scene scene = new Scene(page);
        stage.setScene(scene);
        stage.sizeToScene();
        return (Initializable) loader.getController();
    }

}

2 个答案:

答案 0 :(得分:8)

您需要将要node的{​​{1}}传递给Transition的构造函数。

方法1

所以你可以在FadeTransition

之前使用以下几行并将它们放在你的replaceScene()中
scene = new Scene(page)

虽然欢迎屏幕上也会有一个page = (AnchorPane) loader.load(ins); FadeTransition ft = new FadeTransition(Duration.millis(3000), page); ft.setFromValue(0.0); ft.setToValue(1.0); ft.play(); Scene scene = new Scene(page); ,你必须运用一些逻辑来狂热它!

方法2

Fading Effect加载到fxml /获取gotoSignin()引用中并将其传递给控制器​​。虽然,我相信这会为您当前的设计带来很多变化!你可以接听电话决定!

希望它有所帮助!

答案 1 :(得分:1)

是的,当然,你可以有一个淡出效果 - 只是来自你的FadeTransition的反向值,如:

FadeTransition ft = new FadeTransition(Duration.millis(3000), page);
ft.setFromValue(1);
ft.setToValue(0);
ft.play();