【问题标题】:Why is there extra space in my JavaFX application?为什么我的 JavaFX 应用程序中有额外的空间?
【发布时间】:2016-04-08 18:24:00
【问题描述】:

我有以下申请:

我的应用程序的最右侧有很多不需要的空白。

这是我的start() 方法。在这里,initComponents() 只是实例化了应用程序上的控件,并将其添加到网格窗格中。

primaryStage.setTitle("Music Masher");

gridPane = new GridPane();

initComponents();

root = new StackPane();
root.getChildren().add(gridPane);

primaryStage.setScene(new Scene(root));
primaryStage.sizeToScene();
primaryStage.show(); 

initComponents() 方法中,网格窗格中唯一更改的约束如下:

gridPane.setHgap(10);
gridPane.setVgap(15);
gridPane.setPadding(new Insets(10, 10, 10, 10));

TextFields 的列跨度为 4,一些按钮的列跨度为 2。

initComponents()方法:

public void initComponents(){

    //Left components
    goBackLeft = new Button("<<");
    gridPane.add(goBackLeft, 0, 0);

    playButtonLeft = new Button("    Play    ");
    gridPane.add(playButtonLeft, 1, 0);
    gridPane.setColumnSpan(playButtonLeft, 2);

    goForwardLeft = new Button(">>");
    gridPane.add(goForwardLeft, 3, 0);

    songFieldLeft = new TextField();
    songFieldLeft.setEditable(false);
    gridPane.add(songFieldLeft, 0, 1);
    gridPane.setColumnSpan(songFieldLeft, 4);

    pickButtonLeft = new Button("Pick Song");
    gridPane.add(pickButtonLeft, 1, 2);
    gridPane.setColumnSpan(pickButtonLeft, 2);
    //End Left

    //Right components
    goBackRight = new Button("<<");
    gridPane.add(goBackRight, 5, 0);

    playButtonRight = new Button("    Play    ");
    gridPane.add(playButtonRight, 6, 0);
    gridPane.setColumnSpan(playButtonRight, 2);

    goForwardRight = new Button(">>");
    gridPane.add(goForwardRight, 8, 0);

    songFieldRight = new TextField();
    songFieldRight.setEditable(false);
    gridPane.add(songFieldRight, 5, 1);
    gridPane.setColumnSpan(songFieldRight, 4);

    pickButtonRight = new Button("Pick Song");
    gridPane.add(pickButtonRight, 6, 2);
    gridPane.setColumnSpan(pickButtonRight, 2);
    //End right

    mashButton = new Button("Mash!");
    gridPane.add(mashButton, 4, 3);

    gridPane.setHgap(10);
    gridPane.setVgap(15);
    gridPane.setPadding(new Insets(10, 10, 10, 10));

}

为什么会出现这个多余的空间?有谁知道如何解决这一问题?我试过改变舞台的宽度,但这会使控件偏离其原始位置。

【问题讨论】:

  • 我不明白为什么文本字段的列跨度为 4(从屏幕截图中应该是 3),尽管这不会在右侧创建额外的空间。能贴出网格窗格的所有布局代码吗?
  • 现在将发布。但“播放”按钮的列跨度为 2,总共有 4 个单元格。
  • 好的,但是为什么呢?播放按钮不需要跨越其他列。不过没关系。
  • @James_D 代码已发布,这就是全部内容。

标签: java javafx


【解决方案1】:

我不太确定您为什么会看到您所看到的效果,但我认为问题在于您基本上有几个完全空的列(第 2 列和第 7 列),因此,出于某种原因,与这些列相关的水平间隙被推到右侧并创建空白的水平空间。只是重新处理没有空白列的列似乎可以解决它。

顺便说一句,要使“播放”和“选择歌曲”按钮的宽度相同,请不要尝试用空格填充文本:如果您(或用户)更改字体,这将非常糟糕。相反,允许按钮增长(通过将它们的 maxWidth 设置为 Double.MAX_VALUE),并将它们的 fillWidth 约束设置为 true,以便它们填充其列的宽度。

public void initComponents(){

    //Left components
    goBackLeft = new Button("<<");
    gridPane.add(goBackLeft, 0, 0);

    playButtonLeft = new Button("Play");
    playButtonLeft.setMaxWidth(Double.MAX_VALUE);
    GridPane.setFillWidth(playButtonLeft, true);
    gridPane.add(playButtonLeft, 1, 0);

    goForwardLeft = new Button(">>");
    gridPane.add(goForwardLeft, 2, 0);

    songFieldLeft = new TextField();
    songFieldLeft.setEditable(false);
    gridPane.add(songFieldLeft, 0, 1);
    GridPane.setColumnSpan(songFieldLeft, 3);

    pickButtonLeft = new Button("Pick Song");
    pickButtonLeft.setMaxWidth(Double.MAX_VALUE);
    GridPane.setFillWidth(pickButtonLeft, true);
    gridPane.add(pickButtonLeft, 1, 2);

    //End Left

    //Right components
    goBackRight = new Button("<<");
    gridPane.add(goBackRight, 4, 0);

    playButtonRight = new Button("Play");
    playButtonRight.setMaxWidth(Double.MAX_VALUE);
    GridPane.setFillWidth(playButtonRight, true);
    gridPane.add(playButtonRight, 5, 0);

    goForwardRight = new Button(">>");
    gridPane.add(goForwardRight, 6, 0);

    songFieldRight = new TextField();
    songFieldRight.setEditable(false);
    gridPane.add(songFieldRight, 4, 1);
    GridPane.setColumnSpan(songFieldRight, 3);

    pickButtonRight = new Button("Pick Song");
    pickButtonRight.setMaxWidth(Double.MAX_VALUE);
    GridPane.setFillWidth(pickButtonRight, true);
    gridPane.add(pickButtonRight, 5, 2);

    //End right

    mashButton = new Button("Mash!");
    gridPane.add(mashButton, 3, 3);

    gridPane.setHgap(10);
    gridPane.setVgap(15);
    gridPane.setPadding(new Insets(10, 10, 10, 10));

}

这给了我

【讨论】:

  • 嗯。现在工作。列跨越对我来说有点令人困惑,但我相信我会掌握它。赞赏:)
  • 不要使用多列来尝试改变事物的大小:它不会那样做。请参阅有关使“播放”和“选择”按钮宽度相同的更新。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-01
  • 1970-01-01
  • 2010-11-07
  • 2020-09-04
  • 2013-12-16
相关资源
最近更新 更多