JavaFX的+ MVC。如何隐藏/显示多个窗口? [英] JavaFX+MVC. How to hide/show multiple windows?

查看:219
本文介绍了JavaFX的+ MVC。如何隐藏/显示多个窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我点击打开第二个窗口时,如何使主窗口隐藏起来。当我关闭第二个窗口时,会显示主窗口?我读了这篇帖子,但我不知道不知道如何为我的任务实现它。谢谢!

How to make that when i click on "Open second window" the main window be hide. And when i close second window, the main window be showed? I read this post, but i don't know how to implement it for my task. Thanks!

我有下一个代码:

Main.java

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("First Stage");
        try {
            primaryStage.setResizable(false);

            Parent root = FXMLLoader.load(getClass().getResource("MainView.fxml"));
            Scene scene = new Scene(root);
            primaryStage.setScene(scene);
            primaryStage.show();

        } catch(Exception e) {
            e.printStackTrace();
        }
    }

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

MainWiew.fxml

<?import javafx.geometry.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.BorderPane?>

<BorderPane id="rootMain" prefHeight="418.0" prefWidth="691.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.view.MainController">
   <top>

   </top>
   <center>
      <AnchorPane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
         <children>
            <VBox fillWidth="false" prefHeight="400.0" prefWidth="394.0" spacing="8.0">
               <children>
                  <Button id="btnMainOne" fx:id="btnMainOne" mnemonicParsing="false" prefHeight="51.0" prefWidth="398.0" text="Open second window" textFill="#4460ff">
                     <font>
                        <Font name="Times New Roman Bold" size="20.0" />
                     </font>
                  </Button>

               </children>
               <padding>
                  <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
               </padding>
            </VBox>
         </children>
      </AnchorPane>
   </center>
</BorderPane>

MainController.java

import java.io.IOException;  
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.stage.Modality;
import javafx.stage.Stage;

public class MainController {
    @FXML
    private Button btnMainOne;

    @FXML
    private void initialize() {
        btnMainOne.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {

                try {
                    Stage stage = new Stage();
                    stage.setTitle("Second stage");
                    stage.setScene(new Scene(root, 450, 450));
                    stage.show();

                } catch (IOException e) {
                    e.printStackTrace();
                }


            }
        });

    }
}


推荐答案

您可以通过调用 getScene()。getWindow()来获取主窗口。您可以在显示新阶段后调用 hide()来隐藏它,并且可以在新阶段注册事件监听器,该阶段显示新窗口时的主窗口阶段被隐藏:

You can get the primary window by calling getScene().getWindow(). You can call hide() on it to hide it after you show the new stage, and you can register an event listener with the new stage that shows the primary window when the new stage is hidden:

@FXML
private void initialize() {
    btnMainOne.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent mouseEvent) {

            Stage primaryStage = (Stage)btnMainOne.getScene().getWindow();

            try {
                Stage stage = new Stage();
                stage.setTitle("Second stage");
                stage.setScene(new Scene(root, 450, 450));

                stage.setOnHidden(e -> primaryStage.show());

                stage.show();

                primaryStage.hide();

            } catch (IOException e) {
                e.printStackTrace();
            }


        }
    });

}

这篇关于JavaFX的+ MVC。如何隐藏/显示多个窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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