Store dynamic screen with multiple stages

巧了我就是萌 提交于 2019-12-03 18:16:13

问题


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

回答1:


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



回答2:


if the primaryStage is where "user can load images" do :

static Stage primaryStage;

@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();

    if(primaryStage==null)
    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);

}



来源:https://stackoverflow.com/questions/36585761/store-dynamic-screen-with-multiple-stages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!