【问题标题】:JavaFX more ScenesJavaFX 更多场景
【发布时间】:2015-05-16 01:14:13
【问题描述】:

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

GU1

btn.setOnAction(new EventHandler <ActionEvent>(){

                public void handle(ActionEvent arg0) {

                    try {

                        new GUI2().start(primaryStage);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });

我的问题! 当我按下按钮时,GUI1 还在运行吗?

图形界面2

    btn.setOnAction(new EventHandler <ActionEvent>(){

                public void handle(ActionEvent arg0) {

                    try {
                        //back to the main menu
                        new GUI1().start(primaryStage);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });

当我按下按钮时,它会在启动程序时返回到同一个实例吗?或者让它成为一个具有相同外观的新实例,并使用更多内存;

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

【问题讨论】:

  • 不完全理解您的问题。这些 GUI 是否都有不同的阶段?
  • 每个应用程序实际上应该只有一个start(Stage) 方法(和一个Application 子类)。
  • ok,不用 start() 就可以启动 gui;

标签: user-interface javafx scene


【解决方案1】:

当我按下按钮时,它会在启动程序时返回到同一个实例吗?

不,会根据您的代码new GUI2().start(primaryStage); 创建一个新实例。永远记住new关键字ALWAYS会创建一个新对象。

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

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

方法一

如果您创建了两个应用程序,都扩展了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);
    }

}

...以及其他方法。选择适合您要求的方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-23
    • 2014-04-05
    • 2015-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多