存储多个阶段的动态屏幕 [英] Store dynamic screen with multiple stages

查看:105
本文介绍了存储多个阶段的动态屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个不同的屏幕,在第一个屏幕中,用户可以加载图像,另一个只是一个按钮(舞台是隐形的,所以舞台必须与屏幕1不同)用于返回第一阶段。问题是当我从屏幕2返回到屏幕1时,我不知道如何保持图像加载(当我进入第2阶段时,我隐藏了第1阶段)。这是我在两个屏幕中的当前方法。

I´ve got 2 different screens, in the first screen the user can load images and the other one is just a single button (the stage is invisible so the stage has to be different from screen 1) used to go back to the first stage. The problem is I don't know how to keep the images loaded when I go back from screen 2 to screen 1 (I hide stage 1 when I go to stage 2) . This is my current method in both screens.

    @FXML
private void goToScreen1(ActionEvent event) throws Exception{


        Stage stage = (Stage) showStage.getScene().getWindow();
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/sample.fxml"));
        Parent root = fxmlLoader.load();

        Stage primaryStage = new Stage();

        primaryStage.setResizable(true);
        primaryStage.setOpacity(0.0);
        primaryStage.show();

        primaryStage.setX(0);
        primaryStage.setY(0);
        primaryStage.setHeight(primScreenBounds.getHeight());
        primaryStage.setWidth(primScreenBounds.getWidth() / 2);
}


推荐答案

您必须存储参考到您的舞台内容,所以您不必重新实例化它。
一种可能性是将它存储在mainController中,并为你的subController提供一个EventHandler,以切换舞台

You have to store a reference to your stage content, so you don't have to reinstantiate it. One possibility is to store it in the mainController and provide an EventHandler to your subController, to switch the stage

public interface ScreenController {
        void setOnSwitchStage(EventHandler<ActionEvent> handler);
}

public class Screen1Controller implements Initializable, ScreenController {

    @FXML
    private Button btnSwitchScreen

    public Screen1Controller() {
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
    }

     @Override
    public void setOnSwitchStage(EventHandler<ActionEvent> handler) {
        btnSwitchScreen.setOnAction(handler);
    }
}

public class YourApp extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        Stage secondaryStage = new Stage();

        initScreen(primaryStage, secondaryStage, "screen1.fxml");
        initScreen(secondaryStage, primaryStage, "screen2.fxml");

        primaryStage.show();
    }

    private void initScreen(Stage parentStage, Stage nextStage, String resource) throws IOException {
    FXMLLoader loader = new FXMLLoader(getClass().getResource(resource));
    Scene scene = new Scene(loader.load());
    parentStage.setScene(scene);

    ScreenController screenController = loader.getController();
    screenController.setOnSwitchStage(evt -> {
        nextStage.show();
        parentStage.hide();
    });
}

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

这篇关于存储多个阶段的动态屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆