JAVAFX 舞台会根据场景的变化而改变其大小。或者这就是我的想法

标签 java javafx scenebuilder javafx-11

每当我切换场景时,用户界面就会以这个奇怪的角度被裁剪。它发生在我的实际项目中,但有太多组件需要处理,所以我决定制作一个虚拟的。实际项目VBox作为root,这里是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);
}`

样本.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>

场景2.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);
}

}

场景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);
    }
}

最佳答案

所以你的问题是,当你发布你正在打电话时,你从来没有真正卸下以前的锚定痛苦

root.getChildren()

这意味着您只是将示例锚定 Pane 的内容设置到嵌套它们的另一个锚定 Pane ,从而导致这种奇怪的行为。

试试这个,看看它是否有效

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

这应该可以解决您的问题,如果可以解决问题请告诉我

关于JAVAFX 舞台会根据场景的变化而改变其大小。或者这就是我的想法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60868700/

相关文章:

java - Project Lombok - 自定义生成的 Setter

java - JVM 如何知道何时抛出 NullPointerException

JavaFX:如何更改焦点遍历策略?

java - 如果小于视口(viewport),则扩展滚动 Pane 的内容

javafx - fxml 包括其他 fxml 文件和用户定义的属性

jakarta-ee - JFXtras 和 FXML 互操作性

java - 通过 org.eclipse.ui.menus 扩展点贡献 "editorAction"

java - 使用maven 将javascript 文件打包到Jar 中?

javafx-2 - Javafx:确定节点的字体大小

java - 如何创建自己的按钮并将其与 Scene Builder for javafx 一起使用