JavaFX将新的fxml文件加载到同一场景中

时间:2019-02-20 17:24:23

标签: javafx fxml scene

我有一个名为Test1Controller的类,其中执行了一个Scene,在另一个类Test2Controller中,我想加载另一个FXML,但是要在同一Scene中。有人可以帮我吗?

public class Test1Controller {

    private static AnchorPane page;

    public static Stage initialicePage() {

        primaryStage = new Stage();
        page = (AnchorPane) FXMLLoader.load(Principal.class.getResource("Test1.fxml"));
        Scene scene = new Scene(page);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Test1");
        primaryStage.setResizable(true);
        primaryStage.setMinHeight(700);
        primaryStage.setMinWidth(824);
        primaryStage.setMaximized(true);
        primaryStage.show();
    }
}

1 个答案:

答案 0 :(得分:0)

    public class SwitchScene extends Application {
        @Override
        public void start(Stage stage) throws Exception {
            Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
            Scene scene = new Scene(root);
            stage.setScene(scene);
            stage.show();
        }

        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            launch(args);
        }

    }

    public class FXMLDocumentController implements Initializable {
        @FXML
        private Label label;

        @FXML
        private void handleButtonAction(ActionEvent event) {
            try {
                Parent root = FXMLLoader.load(getClass().getResource("SecondScreen.fxml"));
                Scene dashboard=new Scene(root);
                //This line gets the Stage Information
                Stage window=(Stage)((Node)event.getSource()).getScene().getWindow();
                window.setScene(dashboard);
                window.show();
            } catch (IOException ex) {
                Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        @Override
        public void initialize(URL url, ResourceBundle rb) {
            // TODO
        }    
    }

    public class SecondScreenController implements Initializable {
        /**
         * Initializes the controller class.
         */
        @Override
        public void initialize(URL url, ResourceBundle rb) {
            // TODO
        }    
        public void MoveBack(ActionEvent event){
            try {
                Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

                Scene dashboard = new Scene(root);
                //This line gets the Stage Information
                //here we get the stage from event action and setting the root element in the scene and display scene with stage object (window) which is retrieved from action event
                Stage window=(Stage)((Node)event.getSource()).getScene().getWindow();
                window.setScene(dashboard);
                window.show();
            } catch (IOException ex) {
                Logger.getLogger(SecondScreenController.class.getName()).log(Level.SEVERE, null, ex);
            }


        }
    }`





  //FXMLDocument.xml  
    <?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" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" fx:controller="switchscene.FXMLDocumentController">
        <children>
            <Button layoutX="126" layoutY="90" text="Click Me!" onAction="#handleButtonAction" fx:id="button" />
            <Label layoutX="126" layoutY="120" minHeight="16" minWidth="69" fx:id="label" />
        </children>
    </AnchorPane>



    ////SecondScreen 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="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="switchscene.SecondScreenController">
       <children>
          <Button fx:id="btnMove" layoutX="269.0" layoutY="188.0" mnemonicParsing="false" onAction="#MoveBack" text="Move Back" />
       </children>
    </AnchorPane>