【问题标题】:Vbox not filling up all of the available space in a gridpaneVbox没有填满网格窗格中的所有可用空间
【发布时间】:2019-10-01 06:03:48
【问题描述】:

我目前正在 javafx 中开发一个 Messenger。我的一般布局是一个带有自定义 Vbox 的网格窗格,其中包含一个 ListView 和一个 Textfield。问题是,正如您在下面看到的,文本字段下方有一个很大的空白区域。我已经尝试将 rowspan 设置为 2,但没有成功。

重要的代码:

主要:

chatBox = new ChatBox();
gridPane.add(chatBox, 1, 0, 1, 2);

ChatBox(扩展Vbox):

private static ListView<Message> messages;
private TextField inputField;

public ChatBox() {
    inputField = new TextField();
    messages = new ListView<>();
    init();
    getChildren().addAll(messages, inputField);
}

【问题讨论】:

    标签: java javafx gridpane


    【解决方案1】:

    尝试在 ChatBox 类中添加这个:

     VBox.setVgrow(messages, Priority.ALWAYS);
    

    并在主类中添加:

    GridPane.setVgrow(chatBox, Priority.ALWAYS);
    

    【讨论】:

      【解决方案2】:

      您需要使用RowConstraints 设置vgrow。假设您的第一行包含应该占用所有可用空间的 ListView

      RowConstraints constraints = new RowConstraints();
      constraints.setVgrow(Priority.ALWAYS);
      gridPane.getRowConstraints().addAll(constraints, new RowConstraints());
      

      【讨论】:

        【解决方案3】:

        为了强制列表视图采用其父级中所有可用的高度,可以使用以下方法:

        messages.setMaxHeight(Double.MAX_VALUE);

        如果问题出在VBox,也可以用同样的方法修改它的maxHeight。

        【讨论】:

        • 我用 Vbox、ListView 和 Textfield 试过了,但没有用
        • 您是否同时尝试了所有这些?必须只为 VBox 和 ListView 设置该属性,否则效果将被取消。你能用 setPrefHeight 重试吗?
        猜你喜欢
        • 2018-08-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多