如何使两个FXML布局响应?

时间:2019-04-27 22:49:06

标签: java javafx fxml scenebuilder

我有两个不同的FXML文件和两个Controller类,其中第一个是进入第二个场景的按钮,而第二个场景中是返回第一个场景的后退按钮。如果仅加载其中一个,则布局可以调整大小,但是加载第一个FXML文件,然后单击“播放”按钮后,第二个场景的布局不响应,反之亦然。我的问题是,即使在调整窗口大小之后,如何在前进和后退时都响应这两个布局?

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception {

    try {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("ViewOneScene.fxml"));
        Parent root = (Parent) loader.load();
        Scene scene = new Scene(root);
        primaryStage.setTitle("Test");
                    primaryStage.centerOnScreen();
        primaryStage.setScene(scene);
        primaryStage.show();
    } catch (Exception e) {
        e.printStackTrace();
    }

public class ViewOneController {

@FXML
private Button playButton;

@FXML
private Button optionsButton;

@FXML
private Button exitButton;

@FXML
protected void handlePlayButton(ActionEvent event) throws IOException {
    Parent root = FXMLLoader.load(getClass().getResource("ViewTwoScene.fxml"));
    Scene secondScene = new Scene(root);

    Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
    stage.setScene(secondScene);
    stage.show();
}
<VBox prefHeight="293.0" prefWidth="525.0" style="-fx-background-color: gray;" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="test.ViewOneController">
   <children>
      <MenuBar>
         <menus>
            <Menu mnemonicParsing="false" text="Options">
               <items>
                  <MenuItem mnemonicParsing="false" text="New Game" />
                  <SeparatorMenuItem mnemonicParsing="false" />
                  <MenuItem mnemonicParsing="false" text="Save Game" />
                  <SeparatorMenuItem mnemonicParsing="false" />
                  <MenuItem mnemonicParsing="false" text="6 x 6 Board" />
                  <SeparatorMenuItem mnemonicParsing="false" />
                  <MenuItem mnemonicParsing="false" text="7 x 7 Board" />
               </items>
            </Menu>
            <Menu mnemonicParsing="false" text="Edit" />
            <Menu mnemonicParsing="false" text="Quit" />
         </menus>
      </MenuBar>
      <HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0" VBox.vgrow="ALWAYS">
         <children>
            <VBox alignment="CENTER" prefHeight="200.0" prefWidth="100.0" spacing="20.0">
               <children>
                  <Button fx:id="playButton" mnemonicParsing="false" onAction="#handlePlayButton" prefWidth="100.0" text="Play" />
                  <Button fx:id="optionsButton" mnemonicParsing="false" onAction="#handleOptionsButton" prefWidth="100.0" text="Options" />
                  <Button fx:id="exitButton" mnemonicParsing="false" onAction="#handleExitButton" prefWidth="100.0" text="Exit" />
               </children>
            </VBox>
         </children>
      </HBox>
   </children>
</VBox>

public class ViewTwoController {

@FXML
private Button backButton;

@FXML
protected void handleBackButton(ActionEvent event) throws IOException {
    Parent root = FXMLLoader.load(getClass().getResource("ViewOneScene.fxml"));
    Scene secondScene = new Scene(root);

    Stage window = (Stage) ((Node) event.getSource()).getScene().getWindow();
    window.setScene(secondScene);
    window.show();
}
<VBox style="-fx-background-color: gray;" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="test.ViewTwoController">
  <children>
    <MenuBar VBox.vgrow="NEVER">
      <menus>
        <Menu mnemonicParsing="false" text="Options">
               <items>
                  <MenuItem mnemonicParsing="false" text="New Game" />
                  <SeparatorMenuItem mnemonicParsing="false" />
                  <MenuItem mnemonicParsing="false" text="Save Game" />
                  <SeparatorMenuItem mnemonicParsing="false" />
                  <MenuItem mnemonicParsing="false" text="6 x 6 Board" />
                  <SeparatorMenuItem mnemonicParsing="false" />
                  <MenuItem mnemonicParsing="false" text="7 x 7 Board" />
               </items></Menu>
    <Menu mnemonicParsing="false" text="Edit" />
    <Menu mnemonicParsing="false" text="Quit" />
  </menus>
</MenuBar>
  <HBox alignment="CENTER" VBox.vgrow="ALWAYS">
     <children>
        <VBox alignment="CENTER">
           <children>
              <GridPane alignment="CENTER" gridLinesVisible="true" maxWidth="250.0" prefWidth="200.0">
                <columnConstraints>
                    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                </columnConstraints>
                <rowConstraints>
                    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                </rowConstraints>
                 <children>
                    <StackPane prefHeight="150.0" prefWidth="200.0" />
                    <StackPane prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="1" />
                    <StackPane prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="2" />
                    <StackPane prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="3" />
                    <StackPane prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="4" />
                    <StackPane prefHeight="150.0" prefWidth="200.0" GridPane.rowIndex="1" />
                    <StackPane prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="1" />
                    <StackPane prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="2" GridPane.rowIndex="1" />
                    <StackPane prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="3" GridPane.rowIndex="1" />
                    <StackPane prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="4" GridPane.rowIndex="1" />
                    <StackPane prefHeight="150.0" prefWidth="200.0" GridPane.rowIndex="2" />
                    <StackPane prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="2" />
                    <StackPane prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="2" GridPane.rowIndex="2" />
                    <StackPane prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="3" GridPane.rowIndex="2" />
                    <StackPane prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="4" GridPane.rowIndex="2" />
                    <StackPane prefHeight="150.0" prefWidth="200.0" GridPane.rowIndex="3" />
                    <StackPane prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="3" />
                    <StackPane prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="2" GridPane.rowIndex="3" />
                    <StackPane prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="3" GridPane.rowIndex="3" />
                    <StackPane prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="4" GridPane.rowIndex="3" />
                    <StackPane prefHeight="150.0" prefWidth="200.0" GridPane.rowIndex="4" />
                    <StackPane prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="4" />
                    <StackPane prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="2" GridPane.rowIndex="4" />
                    <StackPane prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="3" GridPane.rowIndex="4" />
                    <StackPane prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="4" GridPane.rowIndex="4" />
                 </children>
              </GridPane>
              <Button fx:id="backButton" mnemonicParsing="false" onAction="#handleBackButton" text="Back" />
           </children>
        </VBox>
     </children>
  </HBox>

0 个答案:

没有答案