我想创建一个方法,它在 vaadin 中创建一个带参数的按钮。
private void addButton(Button button, String toolTip, FontAwesome fontAwesome, ... clickEvent){
button = new Button(fontAwesome);
button.setDescription(toolTip);
button.setEnabled(false);
button.addClickListener(event -> clickEvent);
}
通过电话:
addButton(newButton, "My test Button", FontAwesome.PLUS_SQUARE, someObject.do())
如何使用 lambda 将 clickListener 设置为参数?
最佳答案
private void addButton(Button button, String toolTip, FontAwesome fontAwesome, Button.ClickListener clickEvent){
button = new Button(fontAwesome);
button.setDescription(toolTip);
button.setEnabled(false);
button.addClickListener(clickEvent);
}
然后您将像这样调用您的方法 addButton(newButton, "Test", o, evt -> someMethod());
[编辑]:虽然我认为它回答了您的问题,但请注意,方法调用者无法访问new Button
,这可能是您的错误代码,您应该考虑返回按钮
关于Java 8 使用 lambda 将 clickListener 作为参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44436753/