【问题标题】:JAVAFX Stage changes its size on change in Scene. Or thats what I thinkJAVAFX 舞台随着场景的变化而改变其大小。或者这就是我的想法
【发布时间】:2020-03-26 13:44:07
【问题描述】:

每当我切换场景时,UI 都会以这个奇怪的角度裁剪。它发生在我的实际项目中,但有太多的组件要经过,所以我决定做一个虚拟的。实际项目 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);
}`

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】:

    所以你的问题是你从来没有真正减轻你之前的锚定痛苦,因为你发布了你正在调用的内容

    root.getChildren()
    

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-05
      • 2017-11-08
      • 2018-09-15
      • 1970-01-01
      • 1970-01-01
      • 2019-02-18
      • 1970-01-01
      • 2020-07-02
      相关资源
      最近更新 更多