JAVAFX Stage会根据场景的更改而改变其大小。还是我想的那样

问题描述 投票:0回答:1

[无论何时切换场景,UI都会在这个怪异的角度被裁剪。这是在我的实际项目中发生的,但是要处理的组件太多,所以我决定制作一个虚拟的组件。实际项目VBox作为根目录,这里是AnchorPane。结果完全相同。

目标用户界面:1

更改为场景2:2

切换回场景1: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.fxml:

<?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.fxml:

    <?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>

Controller.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);
}

}

Scene2.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);
    }
}
java javafx scenebuilder javafx-11
1个回答
0
投票

所以您的问题是,在发布呼叫时,您实际上从未卸载过先前的锚痛

root.getChildren()

这意味着您只是将样本锚定窗格的内容设置为另一个锚定窗格,将它们嵌套在一起,从而导致这种奇怪的行为。

尝试一下,看看是否可行

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

这应该可以解决您的问题,让我知道是否可以解决

© www.soinside.com 2019 - 2024. All rights reserved.