首页 文章

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

提问于
浏览
3

我正在使用Javafx(不使用FXML),我正在将舞台传递给控制器,以便在单击按钮时更改舞台上的场景 . 场景变化正确,但舞台和场景的大小增加 . 它的大小增加了大约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);
}

我相信它与我的舞台传递有关,任何指针都会非常感激 .

1 回答

  • 1

    我还发现当我使用 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()));
    }
    

相关问题