【问题标题】:Javafx Scene switching causing stage size increaseJavafx 场景切换导致舞台大小增加
【发布时间】:2016-12-07 13:59:28
【问题描述】:

我正在使用 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);
}

} 我相信这与我的舞台传递有关,任何指点将不胜感激。

【问题讨论】:

  • 创建一个新阶段。
  • 如果我创建一个新阶段,窗口会关闭并重新打开片刻。这不是我想要的流线型设计。
  • 我的猜测是组件不适合舞台原始尺寸。我认为这会导致舞台变大以适应新的组件。
  • 我以为可能是这样,但是新场景和上一个场景一样,只是我改变了背景颜色(所以我可以区分它们)。
  • 替换现有场景的根目录,而不是替换场景。

标签: java javafx scene stage sizing


【解决方案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()));
}

【讨论】:

    猜你喜欢
    • 2018-07-05
    • 1970-01-01
    • 2021-07-21
    • 2016-12-13
    • 2018-04-08
    • 1970-01-01
    • 2019-04-17
    • 2020-03-23
    • 2020-07-02
    相关资源
    最近更新 更多