【问题标题】:JavaFX loading a new fxml file into the same sceneJavaFX 将新的 fxml 文件加载到同一场景中
【发布时间】:2019-07-14 11:34:12
【问题描述】:

我有一个名为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();
    }
}

【问题讨论】:

标签: javafx fxml scene


【解决方案1】:
    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>

【讨论】:

    猜你喜欢
    • 2013-09-08
    • 2016-07-17
    • 2012-04-23
    • 2015-09-08
    • 1970-01-01
    • 1970-01-01
    • 2017-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多