User interface JavaFX更多场景

User interface JavaFX更多场景,user-interface,javafx,scene,User Interface,Javafx,Scene,大家好,我构建了一个GUI,在这个GUI上有一个按钮,当我按下按钮时,会出现第二个GUI,在第二个GUI上也是一个按钮,当我按下按钮时,它会返回 古1 btn.setOnAction(新的EventHandler(){ 公共无效句柄(ActionEvent arg0){ 试一试{ 新GUI2().start(primaryStage); }捕获(例外e){ e、 printStackTrace(); } } }); 我的问题! 当我按下按钮时,GUI 1是否仍在运行 桂2 btn.setOnA

大家好,我构建了一个GUI,在这个GUI上有一个按钮,当我按下按钮时,会出现第二个GUI,在第二个GUI上也是一个按钮,当我按下按钮时,它会返回

古1

btn.setOnAction(新的EventHandler(){
公共无效句柄(ActionEvent arg0){
试一试{
新GUI2().start(primaryStage);
}捕获(例外e){
e、 printStackTrace();
}
}
});
我的问题! 当我按下按钮时,GUI 1是否仍在运行

桂2

btn.setOnAction(新的EventHandler(){
公共无效句柄(ActionEvent arg0){
试一试{
//返回主菜单
新GUI1().start(primaryStage);
}捕获(例外e){
e、 printStackTrace();
}
}
});
当我按下按钮时,它是否返回到启动程序时的同一实例?或者让它成为一个新的例子巫婆有相同的外观,并使用它更多的内存

当我想在外部窗口中打开第二个GUI时,它应该如何工作?当我按下按钮时,它会返回到启动程序时的同一实例吗?

否,将根据您的代码
new GUI2().start(primaryStage)创建一个新实例。始终记住,
new
关键字总是创建一个新对象

当我想在外部窗口中打开第二个GUI时,它应该如何工作?

有很多方法可以做到这一点

方法1

如果您创建了两个应用程序,这两个应用程序都扩展了
Application
类,那么这个方法应该可以工作

public class MultiWindowFX {

    private static final Logger logger = Logger.getGlobal();

    public static class GUI1 extends Application {
        private final Button buttonShowGUI2;
        private final GUI2 gui2;

        public GUI1() {
            buttonShowGUI2 = new Button("Show GUI 2");
            gui2 = new GUI2();
        }

        public Button getButtonShowGUI2() {
            return buttonShowGUI2;
        }

        @Override
        public void start(Stage primaryStage) throws Exception {
            //add an action event on GUI2's buttonShowGUI1 to send front GUI1
                gui2.getButtonShowGUI1().setOnAction(gui2ButtonEvent -> {
                    if (primaryStage.isShowing()) primaryStage.toFront();
                    else primaryStage.show();
                });
            //button with action to show GUI 2
                buttonShowGUI2.setOnAction(actionEvent -> {
                    try {
                        if (gui2.getPrimaryStage() == null) gui2.start(new Stage());
                        else gui2.getPrimaryStage().toFront();
                    } catch (Exception ex) {
                        logger.log(Level.SEVERE, null, ex);
                    }
                });
            //set scene and its root
                Pane root = new StackPane(buttonShowGUI2);
                Scene stageScene = new Scene(root, 400, 250);
            //set stage
                primaryStage.setScene(stageScene);
                primaryStage.centerOnScreen();
                primaryStage.setTitle("GUI 1");
                primaryStage.show();
        }

        public static void launchApp(String... args) {
            GUI1.launch(args);
        }
    }

    public static class GUI2 extends Application {
        private Stage primaryStage;
        private final Button buttonShowGUI1;

        public GUI2() {
            buttonShowGUI1 = new Button("Show GUI 1");
        }

        public Button getButtonShowGUI1() {
            return buttonShowGUI1;
        }

        public Stage getPrimaryStage() {
            return primaryStage;
        }

        @Override
        public void start(Stage primaryStage) throws Exception {
            //get stage reference
                this.primaryStage = primaryStage;
            //set scene and its root
                Pane root = new StackPane(buttonShowGUI1);
                Scene stageScene = new Scene(root, 400, 250);
            //set stage
                primaryStage.setScene(stageScene);
                primaryStage.centerOnScreen();
                primaryStage.setTitle("GUI 2");
                primaryStage.show();
        }

        public static void launchApp(String... args) {
            GUI2.launch(args);
        }
    }

    public static void main(String... args) {
        GUI1.launchApp(args);
    }

}
方法2

对我来说,这是最好的方法,尤其是如果你想要窗口所有权和模态工作的话

public class GUI1 extends Application {

    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        btn.setText("Show GUI2");
        btn.setOnAction(actionEvent -> {
            //prepare gui2
                Stage gui2Stage = createGUI2();
            //set window modality and ownership
                gui2Stage.initModality(Modality.APPLICATION_MODAL);
                gui2Stage.initOwner(primaryStage);
            //show
                gui2Stage.show();
        });

        StackPane root = new StackPane();
        root.getChildren().add(btn);

        Scene scene = new Scene(root, 400, 250);

        primaryStage.setTitle("GUI 1");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private Stage createGUI2() {
        Button btn = new Button();
        btn.setText("Show GUI1");

        StackPane root = new StackPane();
        root.getChildren().add(btn);

        Scene scene = new Scene(root, 300, 150);

        Stage gui2Stage = new Stage();
        gui2Stage.setTitle("GUI 2");
        gui2Stage.setScene(scene);

        //add an action event to GUI2's button, which hides GUI2 and refocuses to GUI1
        btn.setOnAction(actionEvent -> gui2Stage.hide());

        return gui2Stage;
    }

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

}

…以及其他方法。选择适合您需求的方法。

您的问题不太清楚。每个GUI都有不同的阶段吗?每个应用程序应该只有一个
start(阶段)
方法(和一个
Application
子类);
public class MultiWindowFX {

    private static final Logger logger = Logger.getGlobal();

    public static class GUI1 extends Application {
        private final Button buttonShowGUI2;
        private final GUI2 gui2;

        public GUI1() {
            buttonShowGUI2 = new Button("Show GUI 2");
            gui2 = new GUI2();
        }

        public Button getButtonShowGUI2() {
            return buttonShowGUI2;
        }

        @Override
        public void start(Stage primaryStage) throws Exception {
            //add an action event on GUI2's buttonShowGUI1 to send front GUI1
                gui2.getButtonShowGUI1().setOnAction(gui2ButtonEvent -> {
                    if (primaryStage.isShowing()) primaryStage.toFront();
                    else primaryStage.show();
                });
            //button with action to show GUI 2
                buttonShowGUI2.setOnAction(actionEvent -> {
                    try {
                        if (gui2.getPrimaryStage() == null) gui2.start(new Stage());
                        else gui2.getPrimaryStage().toFront();
                    } catch (Exception ex) {
                        logger.log(Level.SEVERE, null, ex);
                    }
                });
            //set scene and its root
                Pane root = new StackPane(buttonShowGUI2);
                Scene stageScene = new Scene(root, 400, 250);
            //set stage
                primaryStage.setScene(stageScene);
                primaryStage.centerOnScreen();
                primaryStage.setTitle("GUI 1");
                primaryStage.show();
        }

        public static void launchApp(String... args) {
            GUI1.launch(args);
        }
    }

    public static class GUI2 extends Application {
        private Stage primaryStage;
        private final Button buttonShowGUI1;

        public GUI2() {
            buttonShowGUI1 = new Button("Show GUI 1");
        }

        public Button getButtonShowGUI1() {
            return buttonShowGUI1;
        }

        public Stage getPrimaryStage() {
            return primaryStage;
        }

        @Override
        public void start(Stage primaryStage) throws Exception {
            //get stage reference
                this.primaryStage = primaryStage;
            //set scene and its root
                Pane root = new StackPane(buttonShowGUI1);
                Scene stageScene = new Scene(root, 400, 250);
            //set stage
                primaryStage.setScene(stageScene);
                primaryStage.centerOnScreen();
                primaryStage.setTitle("GUI 2");
                primaryStage.show();
        }

        public static void launchApp(String... args) {
            GUI2.launch(args);
        }
    }

    public static void main(String... args) {
        GUI1.launchApp(args);
    }

}
public class GUI1 extends Application {

    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        btn.setText("Show GUI2");
        btn.setOnAction(actionEvent -> {
            //prepare gui2
                Stage gui2Stage = createGUI2();
            //set window modality and ownership
                gui2Stage.initModality(Modality.APPLICATION_MODAL);
                gui2Stage.initOwner(primaryStage);
            //show
                gui2Stage.show();
        });

        StackPane root = new StackPane();
        root.getChildren().add(btn);

        Scene scene = new Scene(root, 400, 250);

        primaryStage.setTitle("GUI 1");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private Stage createGUI2() {
        Button btn = new Button();
        btn.setText("Show GUI1");

        StackPane root = new StackPane();
        root.getChildren().add(btn);

        Scene scene = new Scene(root, 300, 150);

        Stage gui2Stage = new Stage();
        gui2Stage.setTitle("GUI 2");
        gui2Stage.setScene(scene);

        //add an action event to GUI2's button, which hides GUI2 and refocuses to GUI1
        btn.setOnAction(actionEvent -> gui2Stage.hide());

        return gui2Stage;
    }

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

}