У меня есть два разных файла 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>