【问题标题】:JavaFX dynamic gridpane row height adjustmentJavaFX 动态网格窗格行高调整
【发布时间】:2017-07-06 07:53:43
【问题描述】:

我目前正在做一个项目,但我找不到任何解决方案。我希望从该行内一个特定列的宽度动态计算 Gridpane 行高。整个 Gridpane 必须可调整大小,其余可用空间应位于下方的另一行,以便根据该单元格内的元素保留该单元格的纵横比。男孩有什么想法吗?

click for a pic

public class GridPaneControl extends GridPane {

    private int index;
    private Label startLabel;
    private Label endLabel;

    private HBox inputCellsContainer;
    private HBox outputCellsContainer;

    private Button inputOutputNextButton;
    private Button inputOutputPreviousButton;

    public GridPaneControl() {

        setGridLinesVisible(true);
        initialization();

        ColumnConstraints cc0 = new ColumnConstraints();
        cc0.setMaxWidth(Double.MAX_VALUE);
        cc0.setHgrow(Priority.NEVER);

        ColumnConstraints cc1 = new ColumnConstraints();
        cc1.setMaxWidth(Double.MAX_VALUE);

        ColumnConstraints cc2 = new ColumnConstraints();
        cc2.setMaxWidth(Double.MAX_VALUE);
        cc2.setHgrow(Priority.NEVER);

        RowConstraints rc0 = new RowConstraints();
        rc0.setVgrow(Priority.ALWAYS);
        rc0.setFillHeight(false);
        rc0.setMaxHeight(Double.MAX_VALUE);

        RowConstraints rc1 = new RowConstraints();
        rc1.setVgrow(Priority.ALWAYS);
        rc1.setFillHeight(false);
        rc1.setMaxHeight(Double.MAX_VALUE);

        RowConstraints rc2 = new RowConstraints();
        rc2.setVgrow(Priority.NEVER);

        RowConstraints rc3 = new RowConstraints();
        rc3.setVgrow(Priority.ALWAYS);

        getColumnConstraints().addAll(cc0, cc1, cc2);
        getRowConstraints().addAll(rc0, rc1, rc2, rc3);

    }

    private void initialization() {

        inputCellsContainer = new HBox(0);
        outputCellsContainer = new HBox(0);

        GridPane.setValignment(inputCellsContainer, VPos.BOTTOM);
        GridPane.setValignment(outputCellsContainer, VPos.TOP);

        inputOutputPreviousButton = new Button("<<");
        inputOutputPreviousButton.maxWidth(Double.MAX_VALUE);
        GridPane.setHgrow(inputOutputPreviousButton, Priority.NEVER);
        GridPane.setVgrow(inputOutputPreviousButton, Priority.NEVER);
        GridPane.setMargin(inputOutputPreviousButton, new Insets(5));

        inputOutputNextButton = new Button(">>");
        inputOutputNextButton.maxWidth(Double.MAX_VALUE);
        GridPane.setHgrow(inputOutputNextButton, Priority.NEVER);
        GridPane.setVgrow(inputOutputNextButton, Priority.NEVER);
        GridPane.setMargin(inputOutputNextButton, new Insets(5));

        for (int i = 0; i < 32; i++) {

            InputOutputCell cellIn = new InputOutputCell(String.format("%02X", i), Color.AQUA, 0);
            InputOutputCell cellOut = new InputOutputCell(String.format("%02X", i), Color.BEIGE, 1);
            HBox.setHgrow(cellIn, Priority.ALWAYS);
            HBox.setHgrow(cellOut, Priority.ALWAYS);

            inputCellsContainer.getChildren().add(cellIn);
            outputCellsContainer.getChildren().add(cellOut);

        }

        GridPane.setHgrow(inputCellsContainer, Priority.ALWAYS);
        GridPane.setHgrow(outputCellsContainer, Priority.ALWAYS);
        GridPane.setVgrow(inputCellsContainer, Priority.ALWAYS);
        GridPane.setVgrow(outputCellsContainer, Priority.ALWAYS);

        startLabel = new Label("0");
        endLabel = new Label("31");
        GridPane.setHalignment(startLabel, HPos.LEFT);
        GridPane.setHalignment(endLabel, HPos.RIGHT);

        this.add(inputOutputPreviousButton, 0, 0, 1, 2);
        this.add(inputCellsContainer, 1, 0);
        this.add(outputCellsContainer, 1, 1);
        this.add(inputOutputNextButton, 2, 0, 1, 2);
        this.add(startLabel, 1, 2);
        this.add(endLabel, 1, 2);

    }

    public int getIndex() {
        return index;
    }

    private class InputOutputCell extends StackPane {

        @FXML
        Text text;
        @FXML
        Rectangle rectangle;

        public InputOutputCell(String text, Color color, int type) {

            setMinSize(0, 0);

            this.text = new Text(text);
            rectangle = new Rectangle();


            if (type == 0) {
                rectangle.widthProperty().bind(inputCellsContainer.widthProperty().divide(32));
                rectangle.heightProperty().bind(inputCellsContainer.widthProperty().divide(32));

            } else {

                rectangle.widthProperty().bind(outputCellsContainer.widthProperty().divide(32));
                rectangle.heightProperty().bind(outputCellsContainer.widthProperty().divide(32));

            }

            rectangle.maxWidth(Double.MAX_VALUE);
            rectangle.maxHeight(Double.MAX_VALUE);
            rectangle.setArcHeight(10);
            rectangle.setArcWidth(10);
            rectangle.setFill(color);
            rectangle.setStroke(Color.BLACK);

            getChildren().addAll(rectangle, this.text);

        }

        public void setText(String text) {
            this.text.setText(text);
        }

        public String getText() {
            return this.text.getText();
        }

    }

}

我希望单元格 1,0 和 1,1 可以调整大小,并且通过增加它们的宽度可以清楚地看到第 0 行和第 1 行的高度不应该平均增加。如果还剩下任何高度,我希望第 3 行占据它,因为第 2 行根本不应该增长到高度。

【问题讨论】:

  • 您应该尝试用更笼统的术语解释您的目标,因为您假设 GridPane 是解决您的问题的正确方法,它可能是也可能不是。
  • 好的,我可以试试。我希望将网格窗格的行高动态调整为其中一列的 1/32 宽度。如果该列的宽度再次发生变化,我希望重新计算该行的宽度。这有可能吗?

标签: java javafx javafx-8


【解决方案1】:

你做了一些解释(这很好),但它足够复杂,让我读了 10 遍,我想我理解了 70%。最令人费解的是这行:

剩余的可用空间应该在下面的另一行,以便根据该单元格内的元素保留该单元格的纵横比。

不确定您指的是哪个“单元格”,以及保持纵横比是什么意思。

现在对于实际答案,我认为我能看到的最明显的一个是您为VGrow 赋予了ALWAYS 的第0 行和第1 行优先级。

您有 3 行包含 ALWAYS VGrowGridPane 将做的是将所有孩子分配到他们希望拥有的任何空间,然后将所有“剩余”空间分配给带有 @ 的行987654326@。这就是为什么图像中的 3 个间隙具有相同高度的原因。

【讨论】:

  • 嘿,我想你正确地理解了我。我指的是单元格 1,0 和 1,1 中的这两个 HBox。这是棘手的部分,Hboxes 内的 stackepanes 的高度是 HBox 本身的 1/32。所以第 1 列需要一直增长到宽度,这很好,但是如何告诉第 0 行和第 1 行只有在 HBox 的高度增加时才增长?第二个问题是增加整个gridpane的宽度引起的,HBoxis中的Stackepanes在增长是没有问题的,但是第1行和第2行的高度是一样的。希望您能理解我,并感谢您之前的回答。
猜你喜欢
  • 1970-01-01
  • 2017-06-20
  • 2011-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-29
  • 2014-08-15
  • 1970-01-01
相关资源
最近更新 更多