JavaFX场景切换导致舞台大小增加

4
我正在使用Javafx(未使用FXML),在单击按钮时将stage传递给控制器以更改舞台上的场景。场景正确更改,但舞台和场景的大小增加了。它的宽度大约增加了0.1,高度有时也增加(不是每次都增加)。
以下是使用的控制器:
 public class Controller {

 public Controller(){

}
 public static void homeButtonhandler(Stage stage){
        stage.close();

     }
 public static void adminButtonhandler(Stage stage){




     adminPane adminPane1 = new adminPane(stage);

    Scene adminScene = new Scene (adminPane1);

     stage.setScene(adminScene);
    }}

adminPane扩展了我创建的另一个类mainPane,两者都扩展了Pane类。它们内部还有其他面板来创建GUI结构。主面板的大小设置如下:

    top = createTop(stage);

    this.getChildren().addAll(top);
    this.setWidth(stage.getWidth());
    this.setPrefWidth(stage.getWidth());
    this.setMaxWidth(stage.getWidth());
    this.setMinWidth(stage.getWidth());

    this.setHeight(stage.getHeight());
    this.setMaxHeight(stage.getHeight());
    this.setMinHeight(stage.getHeight());
    this.setPrefHeight(stage.getHeight());

我正在使用以下方式测试类:

public class test extends Application {

@Override
public void start(Stage primaryStage) throws Exception {
    //primaryStage.setResizable(true);

    mainPane myPane = new mainPane(primaryStage);
    Scene homeScene = new Scene (myPane);

    primaryStage.setScene(homeScene);
    primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("/resources/icons/joulesIcon.png")));
    primaryStage.show();


    }


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

我相信这与我通过阶段有关,非常感谢您提供的任何指导。


创建一个新的舞台。 - SedJ601
我的猜测是某个组件不适合舞台的原始尺寸。我认为这会导致舞台变大以适应新的组件。 - SedJ601
我本以为可能是这样,但新场景与之前的场景相同,只是我改变了背景颜色(这样我就可以区分它们)。 - user2370794
刚在另一个问题上看到这个。http://www.javafxtutorials.com/tutorials/switching-to-different-screens-in-javafx-and-fxml/ - SedJ601
替换根节点 => scene.setRoot(newNode) - jewelsea
显示剩余3条评论
1个回答

2

我还发现当我使用primaryStage.setScene(new Scene(root, primaryStage.getWidth(), primaryStage.getHeight()))时,有些内容被绘制到了窗口底部以下。我的问题是通过使用旧场景的尺寸而不是主舞台的尺寸来解决的。

public void setScene(Parent root) {
    Scene oldScene = primaryStage.getScene();
    primaryStage.setScene(oldScene == null
            ? new Scene(root, primaryStage.getMinWidth(), primaryStage.getMinHeight())
            : new Scene(root, oldScene.getWidth(), oldScene.getHeight()));
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接