Javafx 2 如何在控制器类中交换JavaFX应用程序中的屏幕?

Javafx 2 如何在控制器类中交换JavaFX应用程序中的屏幕?,javafx-2,javafx,Javafx 2,Javafx,如果JavaFX项目中有3个文件;FXML文件、FXML控制器和应用程序类;控制器如何通过更改按钮点击时的屏幕(通常通过stage.setScreen())来响应按钮点击(工作正常)?我没有对stage的引用(它被传递到应用程序类的start(stage)) 应用程序示例: public class JavaFXApplication4 extends Application { @Override public void start(Stage stage) throws E

如果JavaFX项目中有3个文件;FXML文件、FXML控制器和应用程序类;控制器如何通过更改按钮点击时的屏幕(通常通过
stage.setScreen()
)来响应按钮点击(工作正常)?我没有对stage的引用(它被传递到应用程序类的
start(stage)

应用程序示例:

public class JavaFXApplication4 extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("Sample.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();
    }

    /**
     * The main() method is ignored in correctly deployed JavaFX application.
     * main() serves only as fallback in case the application can not be
     * launched through deployment artifacts, e.g., in IDEs with limited FX
     * support. NetBeans ignores main().
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
}
FXML样本:

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="200.0" prefWidth="320.0"  xmlns:fx="http://javafx.com/fxml" fx:controller="javafxapplication4.SampleController">
  <children>
  <Button id="button" fx:id="nextScreen" layoutX="126.0" layoutY="90.0" onAction="#handleButtonAction" text="Next Screen" />
  <Label fx:id="label" layoutX="126.0" layoutY="120.0" minHeight="16.0" minWidth="69.0" />
  </children>
</AnchorPane>

当我进入Java并试图解决同样的问题时,我发现了这个老问题。 因为我想让场景记住开关之间的内容,所以我不能使用接受的答案,因为在场景之间切换时,它会再次实例化它们(失去它们以前的状态)

不管怎样,被接受的答案和答案给了我一些提示,告诉我如何在不失去状态的情况下切换场景。 其主要思想是将场景的实例注入到另一个控制器中,这样控制器就不需要反复实例化一个新的场景,而是可以使用已经存在的实例(及其状态)

下面是实例化场景的主类:

public class Main extends Application {

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

    @Override
    public void start(Stage primaryStage) throws Exception {
        // getting loader and a pane for the first scene. 
        // loader will then give a possibility to get related controller
        FXMLLoader firstPaneLoader = new FXMLLoader(getClass().getResource("firstLayout.fxml"));
        Parent firstPane = firstPaneLoader.load();
        Scene firstScene = new Scene(firstPane, 300, 275);

        // getting loader and a pane for the second scene
        FXMLLoader secondPageLoader = new FXMLLoader(getClass().getResource("secondLayout.fxml"));
        Parent secondPane = secondPageLoader.load();
        Scene secondScene = new Scene(secondPane, 300, 275);

        // injecting second scene into the controller of the first scene
        FirstController firstPaneController = (FirstController) firstPaneLoader.getController();
        firstPaneController.setSecondScene(secondScene);

        // injecting first scene into the controller of the second scene
        SecondController secondPaneController = (SecondController) secondPageLoader.getController();
        secondPaneController.setFirstScene(firstScene);

        primaryStage.setTitle("Switching scenes");
        primaryStage.setScene(firstScene);
        primaryStage.show();
    }
}
以下是两个控制器:

public class FirstController {

    private Scene secondScene;

    public void setSecondScene(Scene scene) {
        secondScene = scene;
    }

    public void openSecondScene(ActionEvent actionEvent) {
        Stage primaryStage = (Stage)((Node)actionEvent.getSource()).getScene().getWindow();
        primaryStage.setScene(secondScene);
    }
}
是的,第二个看起来是一样的(一些逻辑可能是共享的,但当前状态足以作为概念证明)


你也可以这样试试

public void onBtnClick(ActionEvent event) {
    try {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("login.fxml"));
        Stage stage = (Stage) btn.getScene().getWindow();
        Scene scene = new Scene(loader.load());
        stage.setScene(scene);
    }catch (IOException io){
        io.printStackTrace();
    }

}

非常感谢,这正是我想要的:)这非常有帮助!谢谢:)当您在“stage.setScene(…)”结尾处键入时,“stage”必须是两个“stageEventSourceNodeBelongs”或“stageTheLabelLongs”中的一个“对吗?天哪,这救了我的命!我一直在互联网上搜寻类似的东西,因为所有其他示例每次都从文件中加载资源,但我需要保留场景对象,所以谢谢!正是我需要的!一旦解释清楚了就很明显了。非常感谢。
public class FirstController {

    private Scene secondScene;

    public void setSecondScene(Scene scene) {
        secondScene = scene;
    }

    public void openSecondScene(ActionEvent actionEvent) {
        Stage primaryStage = (Stage)((Node)actionEvent.getSource()).getScene().getWindow();
        primaryStage.setScene(secondScene);
    }
}
public class SecondController {

    private Scene firstScene;

    public void setFirstScene(Scene scene) {
        firstScene = scene;
    }

    public void openFirstScene(ActionEvent actionEvent) {    
        Stage primaryStage = (Stage)((Node)actionEvent.getSource()).getScene().getWindow();
        primaryStage.setScene(firstScene);
    }
}
public void onBtnClick(ActionEvent event) {
    try {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("login.fxml"));
        Stage stage = (Stage) btn.getScene().getWindow();
        Scene scene = new Scene(loader.load());
        stage.setScene(scene);
    }catch (IOException io){
        io.printStackTrace();
    }

}