我习惯了 Swing,现在开始接触 FX。
我遇到了一个问题,我在阅读 Oracle 的“在 JavaFX 中使用布局”指南并在互联网上做了一些研究时找不到答案。

在 GridPane 类的 FX API 指南中,有一个关于布置对象的示例:

这是 http://docs.oracle.com/javafx/2/api/javafx/scene/layout/GridPane.html 的摘录:
GridPane gridpane = new GridPane();

 // Set one constraint at a time...
 Button button = new Button();
 GridPane.setRowIndex(button, 1);
 GridPane.setColumnIndex(button, 2);

...

 // don't forget to add children to gridpane
 gridpane.getChildren().addAll(button, label);

行列信息是通过 GridPane 的静态方法设置的。这也是文档所说的。我想了解此布局约束在哪里绑定(bind)到节点对象 - 在这种情况下是按钮。

Node API 文档没有提到布局约束。
我找到了很多关于设置约束的信息,例如为 GridPane 对象上的列,但我找不到关于此的更多信息。

那么行/列信息是如何绑定(bind)到按钮的,或者如何在应用后从按钮中检索这些信息?

最好的毕业生
君特

最佳答案

通读 javaFX 源代码, GridPane 的 setRowIndex 和 setColumnIndex 使用其父类(super class) Pane 的 setConstraint 方法,如下所示:

static void setConstraint(Node node, Object key, Object value) {
        if (value == null) {
            node.getProperties().remove(key);
        } else {
            node.getProperties().put(key, value);
        }
        if (node.getParent() != null) {
            node.getParent().requestLayout();
        }
    }

所以信息直接存储在节点中。

关于java fx : Where is the constraint property saved when using GridPane. setRowIndex(...);,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17136694/

10-13 09:01