JavaFX:将多个参数传递给事件处理程序

JavaFX : Pass multiple parameters to eventhandler

向控制器的EventHandler传递多个参数最合适的方式是什么class?

我正在为我开发的应用程序开发日历模式。我的要求是将月份和年份分别传递给控制器​​。这是我目前的做法。

FXML :

<Button onAction="#handleClicks"  text="DECEMBER" textFill="WHITE" userData="2018/12">

控制器:

public class CalendarController implements Initializable {

public void handleMonths(ActionEvent ev){
    Node node = (Node) ev.getSource();
    String full_mnth = node.getUserData().toString();
    String[] date = full_mnth.split("/");
    System.out.println("Year : "+date[0] +" Month : "+date[1]);
}
}

但是如果我可以使用数组作为 UserData 或者如果我可以通过 多个参数。有人可以建议最合适的方法来实现这一目标。

使用Node.properties地图。这允许您使用字符串作为键来存储对象。请确保不要使用类似于父项的 static 属性的键(指的是此处的 AnchorPane.leftAnchor),因为这些属性也存储在此映射中:

fxml

<Button onAction="#handleClicks" text="DECEMBER" textFill="WHITE">
    <properties>
        <year>
            <Integer fx:value="2018"/>
        </year>
        <month>
            <Integer fx:value="12"/>
        </month>
    </properties>
</Button>

控制器

@FXML
private void handleClicks(ActionEvent event) {
    Map<Object, Object> properties = ((Node) event.getSource()).getProperties();
    System.out.println("year: "+properties.get("year"));
    System.out.println("month: "+properties.get("month"));
}