gpt4 book ai didi

Javafx:绑定(bind) Hbox 间距?

转载 作者:行者123 更新时间:2023-12-02 02:41:35 24 4
gpt4 key购买 nike

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

enter image description here

最佳答案

将每个标签的 minWidthprefWidth 属性绑定(bind)到相应列的 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() 方法,以根据列的宽度定位标签。注册一个监听器以请求 Pane 上每列宽度的布局:

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);
}
}

关于Javafx:绑定(bind) Hbox 间距?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45368956/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com