Javafx:绑定Hbox间距?

Javafx:绑定Hbox间距?,java,javafx,javafx-8,bind,spacing,Java,Javafx,Javafx 8,Bind,Spacing,我有一个TableView和一个Hbox,在Hbox中有三个标签s一个包含文本,两个包含表中两列的总和。我想为HBox设置一个动态的间距,以便将两个标签正好对齐在它们所属的表中两列的下方。是否有可能将HBox的间距绑定到列位置。 我也接受任何其他解决方案,将标签固定在各个列的正下方。 这是一张我想要的图片: 将每个标签的minWidth和prefWidth属性绑定到相应列的width属性: import javafx.application.Application; import javafx.

我有一个
TableView
和一个
Hbox
,在Hbox中有三个
标签
s一个包含文本,两个包含表中两列的总和。我想为HBox设置一个动态的间距,以便将两个标签正好对齐在它们所属的表中两列的下方。是否有可能将
HBox
的间距绑定到列位置。 我也接受任何其他解决方案,将标签固定在各个列的正下方。 这是一张我想要的图片:


将每个标签的
minWidth
prefWidth
属性绑定到相应列的
width
属性:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class TableWithLabelsBelowColumns extends Application {

    @Override
    public void start(Stage primaryStage) {
        TableView<Void> table = new TableView<>();
        table.getItems().add(null);

        TableColumn<Void, Void> firstNameColumn = new TableColumn<>("First Name");
        TableColumn<Void, Void> lastNameColumn = new TableColumn<>("Last Name");
        TableColumn<Void, Void> emailColumn = new TableColumn<>("Email");
        table.getColumns().add(firstNameColumn);
        table.getColumns().add(lastNameColumn);
        table.getColumns().add(emailColumn);

        Label fnLabel = new Label("FN");
        Label lnLabel = new Label("LN");
        Label emailLabel = new Label("E");

        fnLabel.prefWidthProperty().bind(firstNameColumn.widthProperty());
        fnLabel.minWidthProperty().bind(firstNameColumn.widthProperty());
        lnLabel.prefWidthProperty().bind(lastNameColumn.widthProperty());
        lnLabel.minWidthProperty().bind(lastNameColumn.widthProperty());
        emailLabel.prefWidthProperty().bind(emailColumn.widthProperty());
        emailLabel.minWidthProperty().bind(emailColumn.widthProperty());

        HBox labels = new HBox(fnLabel, lnLabel, emailLabel);

        BorderPane root = new BorderPane(table);
        root.setBottom(labels);

        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();

    }

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

将每个标签的
minWidth
prefWidth
属性绑定到相应列的
width
属性:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class TableWithLabelsBelowColumns extends Application {

    @Override
    public void start(Stage primaryStage) {
        TableView<Void> table = new TableView<>();
        table.getItems().add(null);

        TableColumn<Void, Void> firstNameColumn = new TableColumn<>("First Name");
        TableColumn<Void, Void> lastNameColumn = new TableColumn<>("Last Name");
        TableColumn<Void, Void> emailColumn = new TableColumn<>("Email");
        table.getColumns().add(firstNameColumn);
        table.getColumns().add(lastNameColumn);
        table.getColumns().add(emailColumn);

        Label fnLabel = new Label("FN");
        Label lnLabel = new Label("LN");
        Label emailLabel = new Label("E");

        fnLabel.prefWidthProperty().bind(firstNameColumn.widthProperty());
        fnLabel.minWidthProperty().bind(firstNameColumn.widthProperty());
        lnLabel.prefWidthProperty().bind(lastNameColumn.widthProperty());
        lnLabel.minWidthProperty().bind(lastNameColumn.widthProperty());
        emailLabel.prefWidthProperty().bind(emailColumn.widthProperty());
        emailLabel.minWidthProperty().bind(emailColumn.widthProperty());

        HBox labels = new HBox(fnLabel, lnLabel, emailLabel);

        BorderPane root = new BorderPane(table);
        root.setBottom(labels);

        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();

    }

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

与您的问题有关吗?与您的问题有关吗?我刚刚尝试过,但当我尝试使用第三和第四列时,它不起作用,尤其是第一个解决方案。第二个可能会起作用,我必须玩一点数字
fnLabel.resizeRelocate(这里和她,fnWidth,fnHeight)
起初尝试使用此解决方案:,但问题是行是表中的一项,因此当我排序、筛选等时,它会被排序、筛选。最后,我使用您的第二个建议解决了此问题,我首先还添加了
.resizeRelocate()
上一列的宽度,因此它现在工作正常。谢谢。我刚刚试过,但当我尝试使用第三和第四列时,它不起作用,尤其是第一个解决方案。第二个可能会起作用,我必须玩一点数字
fnLabel.resizeRelocate(这里和她,fnWidth,fnHeight)
起初尝试使用此解决方案:,但问题是行是表中的一项,因此当我排序、筛选等时,它会被排序、筛选。最后,我使用您的第二个建议解决了此问题,我首先还添加了
.resizeRelocate()
上一列的宽度,因此它现在工作正常。谢谢