JavaFX загружает новый файл fxml в ту же сцену - PullRequest
0 голосов
/ 20 февраля 2019

У меня есть класс с именем 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 голосов
/ 21 февраля 2019
    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>
...