JavaFX - сохранить панель инструментов при загрузке следующей сцены - PullRequest
0 голосов
/ 11 мая 2018

Как сохранить панель инструментов при загрузке новой сцены?

Пока это мой код:

Main.class

    public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("Main.fxml"));
        primaryStage.setTitle("Test FXML application");
        primaryStage.setScene(new Scene(root, 400, 400));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

Main.fxml

<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ToolBar?>
<GridPane fx:controller="main.java.Controller"
          xmlns:fx="http://javafx.com/fxml">
    <ToolBar prefHeight="40.0" prefWidth="400.0">
        <Button text="testButton" onAction="#testButton"/>
    </ToolBar>
</GridPane>

Controller.class

Я думаю, что проблема здесь.То, как я генерирую следующую сцену, немного сломано.

public class Controller {
    @FXML
    private void testButton1() throws IOException {
        Parent window1;
        try {
            window1 = FXMLLoader.load(getClass().getResource("src/testButton1.fxml"));
            Stage window1Stage;
            Scene window1Scene = new Scene(window1, 400, 400);
            window1Stage = Main.main();
            window1Stage.setScene(window1Scene);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Ответы [ 2 ]

0 голосов
/ 11 мая 2018

Вам нужно иметь отдельную FXML для основной сцены, которая содержит панель инструментов, затем, используя BorderPane, вы можете разделить панель инструментов и другие экраны

См. Пример ниже,

Main.class

public class Main extends Application {
  private static Stage pStage;

  // Creating a static root to pass to the controller
  private static BorderPane root = new BorderPane();


  @Override
  public void start(Stage primaryStage) throws Exception {
    Parent main = FXMLLoader.load(getClass().getResource("Main.fxml"));

    AnchorPane screen1 = FXMLLoader.load(getClass().getResource("screen1.fxml"));

    root.setTop(main);
    root.setCenter(screen1);

    primaryStage.setTitle("Test FXML application");
    primaryStage.setScene(new Scene(root, 400, 400));
    primaryStage.show();
  }

  public static void main(String[] args) {
    launch(args);
  }

  /**
   * Just a root getter for the controller to use
   */
  public static BorderPane getRoot() {
    return root;
  }

Controller.class

public class Controller {
  @FXML
  private void testButton1() throws IOException {
    try {
      AnchorPane screen1 = FXMLLoader.load(getClass().getResource("screen1.fxml"));
      BorderPane border = Main.getRoot();
      border.setCenter(screen1);
    }
    catch (IOException e) {
      e.printStackTrace();
    }
  }
  @FXML
  private void testButton2() throws IOException {
    try {
      AnchorPane screen2 = FXMLLoader.load(getClass().getResource("screen2.fxml"));
      BorderPane border = Main.getRoot();
      border.setCenter(screen2);
    }
    catch (IOException e) {
      e.printStackTrace();
    }
  }
}

Main.fxml (Созданостроитель сцены)

 <?import javafx.scene.control.*?>
 <?import javafx.scene.layout.*?>

    <GridPane prefHeight="0.0" prefWidth="402.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Main.Controller">
        <children>
            <Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="37.0" prefWidth="402.0">
             <children>
                    <ToolBar prefHeight="0.0" prefWidth="400.0">
                        <items>
                            <Button onAction="#testButton1" text="SCREEN 1" />
                            <Button onAction="#testButton2" text="SCREEN 2" />
                        </items>
                    </ToolBar>
             </children>
            </Pane>
        </children>
        <columnConstraints>
            <ColumnConstraints />
        </columnConstraints>
        <rowConstraints>
            <RowConstraints maxHeight="200.0" minHeight="34.0" prefHeight="34.0" />
            <RowConstraints maxHeight="166.0" minHeight="0.0" prefHeight="166.0" />
        </rowConstraints>
    </GridPane>

Сцена 1

<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="210.0" prefWidth="407.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
    <children>
        <Label layoutX="10.0" layoutY="10.0" prefHeight="36.0" prefWidth="374.0" text="SCREEN 1">
            <font>
                <Font name="System Bold" size="36.0" />
            </font>
        </Label>
    </children>
</AnchorPane>

Сцена 2

<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="210.0" prefWidth="407.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
    <children>
        <Label layoutX="10.0" layoutY="10.0" prefHeight="36.0" prefWidth="374.0" text="SCREEN 2">
            <font>
                <Font name="System Bold" size="36.0" />
            </font>
        </Label>
    </children>
</AnchorPane>

Outпут будет такой

enter image description here

0 голосов
/ 11 мая 2018

Для замены части содержимого есть более подходящие макеты, чем GridPane, но в основном вам необходимо использовать родительский элемент для ToolBar, который позволяет заменить дополнительного потомка.В этом случае BorderPane, вероятно, будет хорошей идеей, но это может быть сделано и с GridPane;это просто менее элегантно.

<BorderPane fx:id="container" fx:controller="main.java.Controller"
          xmlns:fx="http://javafx.com/fxml">
    <top>
        <ToolBar prefHeight="40.0" prefWidth="400.0">
            <Button text="testButton" onAction="#testButton"/>
        </ToolBar>
    </top>
</BorderPane>
@FXML
private BorderPane container;

@FXML
private void testButton1() throws IOException {
    try {
        container.setCenter(FXMLLoader.load(getClass().getResource("src/testButton1.fxml")));
        container.getScene().getWindow().sizeToScene(); // resize scene to fit the full size of the content
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Если вы продолжаете использовать GridPane, введите его, используя fx:id, убедитесь, что ToolBar - единственный ребенок, которого вы хотите сохранить, этопервый ребенок и единственный ребенок, расположенный в rowIndex=0, columnIndex=0), вы также можете вручную удалить дополнительных детей:

@FXML
private GridPane container;

@FXML
private void testButton1() throws IOException {
    ObservableList<Node> children = container.getChildren();
    children.remove(1, children.size()); // remove every child after the first
    try {
        container.add(FXMLLoader.load(getClass().getResource("src/testButton1.fxml")), 0, 1);
        container.getScene().getWindow().sizeToScene(); // resize scene to fit the full size of the content
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...