2016-02-19 138 views
1

如何做到这一点,当我点击“打开第二个窗口”主窗口被隐藏。当我关闭第二个窗口时,显示主窗口?我读了这post,但我不知道如何执行它为我的任务。谢谢!JavaFX + MVC。如何隐藏/显示多个窗口?

我有下面的代码:
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(); 
       } 


      } 
     }); 

    } 
} 

回答

3

您可以拨打getScene().getWindow()来获得主窗口。您可以拨打hide()它来隐藏它告诉你的新阶段后,你可以用新的阶段,显示在新的阶段隐藏在主窗口中注册事件监听器:

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


     } 
    }); 

} 
+0

谢谢,它的工作! – Tomas

+0

谢谢,setOnHidden()的作品。 – LZH