表下面有一个TableView和一个Hbox,在hBox中有三个Label--一个包含文本,两个包含表中两列的总和。我想以某种方式为HBox设置一个动态的间距,使两个标签对齐在它们所属的表中的两列下面。是否有可能将HBox的间距绑定到列的位置。我还接受任何其他解决方案,将标签固定在相应的列下面。下面是我想要的图像:

发布于 2017-07-28 19:16:29
将每个标签的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);
}
}另一种方法是子类Pane并重写layoutChildren()方法,以便根据列的宽度来定位标签。注册侦听器以请求窗格上的布局,并使用每一列的宽度:
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
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.Pane;
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");
Pane labelPane = new Pane(fnLabel, lnLabel, emailLabel) {
@Override
protected void layoutChildren() {
double fnWidth = firstNameColumn.getWidth();
double fnHeight = fnLabel.prefHeight(fnWidth);
fnLabel.resizeRelocate(0, 0, fnWidth, fnHeight);
double lnWidth = lastNameColumn.getWidth();
double lnHeight = lnLabel.prefHeight(lnWidth);
lnLabel.resizeRelocate(fnWidth, 0, lnWidth, lnHeight);
double emailWidth = emailColumn.getWidth();
double emailHeight = emailLabel.prefHeight(emailWidth);
emailLabel.resizeRelocate(fnWidth+lnWidth, 0, emailWidth, emailHeight);
}
};
ChangeListener<? super Number> listener = (obs, oldValue, newValue) -> labelPane.requestLayout();
firstNameColumn.widthProperty().addListener(listener);
lastNameColumn.widthProperty().addListener(listener);
emailColumn.widthProperty().addListener(listener);
BorderPane root = new BorderPane(table);
root.setBottom(labelPane);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}https://stackoverflow.com/questions/45368956
复制相似问题