JAVAFX Stage меняет свой размер при изменении в Scene. Или вот что я думаю - PullRequest
0 голосов
/ 26 марта 2020

Всякий раз, когда я переключаю Сцены, пользовательский интерфейс обрезается под этим странным углом. Это происходило в моем реальном проекте, но было слишком много компонентов для go, поэтому я решил сделать фиктивный. Фактический проект VBox как root, здесь это AnchorPane. Результаты точно такие же.

Назначенный интерфейс: 1

Изменить на scene2: 2

Переключиться обратно на scene1: 3

Main. java `

public void start(Stage primaryStage) throws Exception{
    AnchorPane pane = FXMLLoader.load(getClass().getResource("sample.fxml"));
    Scene scene = new Scene(pane);
    primaryStage.setScene(scene);
    primaryStage.setResizable(true);

    primaryStage.show();
    primaryStage.setFullScreen(true);
    primaryStage.setMaximized(true);
}`

sample.f xml:

<?xml version="1.0" encoding="UTF-8"?>

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

<AnchorPane fx:id="root" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <children>
      <SplitPane dividerPositions="0.29797979797979796" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
        <items>
          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
               <children>
                  <Button mnemonicParsing="false" onAction="#loadScene2" text="Go to Scene 2" />
               </children></AnchorPane>
          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0" />
        </items>
      </SplitPane>
   </children>
</AnchorPane>

scene2.f xml:

    <?xml version="1.0" encoding="UTF-8"?>

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

<AnchorPane fx:id="root" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Scene2">
   <children>
      <SplitPane dividerPositions="0.29797979797979796" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
        <items>
          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0" />
          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
               <children>
                  <Button mnemonicParsing="false" onAction="#loadScene2" text="Go to scene 1" />
               </children></AnchorPane>
        </items>
      </SplitPane>
   </children>
</AnchorPane>

Контроллер. java:

public class Controller {
@FXML
private AnchorPane root;
@FXML
private void loadScene2() throws IOException {
    AnchorPane pane = FXMLLoader.load(getClass().getResource("scene2.fxml"));
    root.getChildren().setAll(pane);
}

}

Сцена2. java:

public class Scene2 {
    @FXML
    private AnchorPane root;
    @FXML
    private void loadScene2() throws IOException {
        AnchorPane pane = FXMLLoader.load(getClass().getResource("sample.fxml"));
        root.getChildren().setAll(pane);
    }
}

1 Ответ

0 голосов
/ 26 марта 2020

Итак, ваша проблема в том, что вы никогда не выгружаете свою прежнюю боль привязки, когда вы публикуете, что звоните

root.getChildren()

это означает, что вы просто устанавливаете содержимое своей примерной панели привязки на другую область привязки вложение их приводит к этому странному поведению.

Попробуйте вместо этого и посмотрите, работает ли оно

Parent parent = root.getParent();
parent.getChildren().remove(root);
parent.getChildren().add(pane);

Это должно решить вашу проблему, дайте мне знать, если это решит

...