代码之家  ›  专栏  ›  技术社区  ›  Choxmi

JavaFX:将多个参数传递给eventhandler

  •  0
  • Choxmi  · 技术社区  · 7 年前

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

    <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]);
    }
    }
    

    但是如果我可以使用数组作为用户数据或者我可以传递 多个参数。有人能提出最合适的方法来实现这一点吗。

    1 回复  |  直到 7 年前
        1
  •  4
  •   fabian    7 年前

    使用 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"));
    }