javafx gridpane居中对齐并对齐所有标签

javafx gridpane居中对齐并对齐所有标签,java,javafx,alignment,label,gridpane,Java,Javafx,Alignment,Label,Gridpane,我们的想法是创建一个提交表单,该表单有许多标签居中且对齐,例如: 标签1是客户名称,标签2是产品名称我要做的是将所有标签水平居中对齐,这很容易,但同时我希望它们对齐。简而言之,P应正好位于网格窗格第二行的C下方。请注意,每列中最宽的节点负责确定起始x坐标。如果节点居中,则需要将所有节点移动到左侧最大宽度,移动幅度为节点宽度差的一半,这可以通过使用translateX属性实现: 示例 @Override public void start(Stage primaryStage) { Gri

我们的想法是创建一个提交表单,该表单有许多标签居中且对齐,例如:


标签1是客户名称,标签2是产品名称我要做的是将所有标签水平居中对齐,这很容易,但同时我希望它们对齐。简而言之,P应正好位于网格窗格第二行的C下方。

请注意,每列中最宽的节点负责确定起始x坐标。如果节点居中,则需要将所有节点移动到左侧最大宽度,移动幅度为节点宽度差的一半,这可以通过使用

translateX
属性实现:

示例

@Override
public void start(Stage primaryStage) {
    GridPane gp = new GridPane();
    ColumnConstraints constraints = new ColumnConstraints();
    constraints.setHalignment(HPos.CENTER);
    constraints.setPercentWidth(50);

    gp.getColumnConstraints().addAll(constraints, constraints);
    Random random = new Random();

    Node[][] elements = new Node[2][10];

    for (int x = 0; x < elements.length; x++) {
        final Node[] column = elements[x];
        InvalidationListener sizeListener = o -> {
            // determine max width
            double maxSize = 0;

            for (Node n : column) {
                double width = n.getLayoutBounds().getWidth();
                if (width > maxSize) {
                    maxSize = width;
                }
            }

            // adjust translate
            for (Node n : column) {
                n.setTranslateX((n.getLayoutBounds().getWidth() - maxSize) / 2);
            }
        };

        // fill column with strings of random lenght
        for (int y = 0; y < 10; y++) {
            int charCount = random.nextInt(30);
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < charCount; i++) {
                sb.append('a');
            }
            Label text = new Label(sb.toString());
            text.layoutBoundsProperty().addListener(sizeListener);
            column[y] = text;
        }
        gp.addColumn(x, column);
        sizeListener.invalidated(null);
    }

    Scene scene = new Scene(gp);

    primaryStage.setScene(scene);
    primaryStage.show();
}


如果我用scene builder创建了UI,那么需要对上述内容进行哪些更改code@Takasur由于这不是受支持的标准对齐方式,因此不能简单地在SceneBuilder中设置。您可以使用静态方法将此类行为添加到
GridPane
,请参见我的编辑。
public final class WeirdAlign {

    private WeirdAlign() {
    }

    public static final Predicate<Node> ALL_PREDICATE = n -> true;

    public static void setCombinedCenterJustify(GridPane gridPane, Predicate<Node> predicate) {
        InvalidationListener listener = o -> {
            Node n = (Node) ((ReadOnlyProperty) o).getBean();
            updateGridPaneColumn(gridPane, getGridPaneColumn(n), predicate);
        };
        ObservableList<Node> children = gridPane.getChildren();

        for (Node n : children) {
            if (predicate.test(n)) {
                n.layoutBoundsProperty().addListener(listener);
            }
        }

        int[] columns = children.stream().filter(predicate).mapToInt(WeirdAlign::getGridPaneColumn).distinct().toArray();
        for (int i : columns) {
            updateGridPaneColumn(gridPane, i, predicate);
        }

        children.addListener((ListChangeListener.Change<? extends Node> c) -> {
            Set<Integer> columnsToUpdate = new HashSet<>();
            while (c.next()) {
                if (c.wasRemoved()) {
                    for (Node n : c.getRemoved()) {
                        if (predicate.test(n)) {
                            n.layoutBoundsProperty().removeListener(listener);
                            columnsToUpdate.add(getGridPaneColumn(n));
                        }
                    }
                }
                if (c.wasAdded()) {
                    for (Node n : c.getAddedSubList()) {
                        if (predicate.test(n)) {
                            n.layoutBoundsProperty().addListener(listener);
                            columnsToUpdate.add(getGridPaneColumn(n));
                        }
                    }
                }
            }
            for (Integer i : columnsToUpdate) {
                updateGridPaneColumn(gridPane, i, predicate);
            }
        });
    }

    /**
     * This method is only here for FXMLLoader.
     */
    public static Predicate<Node> getCombinedCenterJustify(GridPane node) {
        throw new UnsupportedOperationException();
    }

    public static void updateGridPaneColumn(GridPane gridPane, int column, Predicate<Node> predicate) {
        double maxSize = 0;
        for (Node child : gridPane.getChildren()) {
            if (column == getGridPaneColumn(child) && predicate.test(child)) {
                double width = child.getLayoutBounds().getWidth();
                if (width > maxSize) {
                    maxSize = width;
                }
            }
        }
        for (Node child : gridPane.getChildren()) {
            if (column == getGridPaneColumn(child) && predicate.test(child)) {
                child.setTranslateX((child.getLayoutBounds().getWidth() - maxSize) / 2);
            }
        }
    }

    public static int getGridPaneColumn(Node node) {
        Integer c = GridPane.getColumnIndex(node);
        return c == null ? 0 : c;
    }

}