FXML 中的 JavaFX .getProperties()

JavaFX .getProperties() in FXML

我怎样才能将其移动到 FXML?

hBox.getProperties().put("key", "value");

你可以做到

<HBox>
    <properties key="value"/>
</HBox>

documentation.

变化

<HBox>
    <properties>
        <key>
            <String fx:value="value"/>
        </key>
    </properties>
</HBox>

如果您想要的值是一个更复杂的对象,可能会有用:

<HBox>
    <properties>
        <character>
            <String fx:value="Arthur Dent"/>
        </character>
        <actor>      
            <Actor firstName="Simon" lastName="Jones"/>
        </actor>
    <properties>
</HBox>

相当于

Actor actor = new Actor();
actor.setFirstName("Simon");
actor.setLastName("Jones");
HBox hbox = new HBox();
hbox.getProperties().put("character", "Arthur Dent");
hbox.getProperties().put("actor", actor);