JavaFx - 将两个 'setOnAction' 设置为同一个按钮
JavaFx - Set two 'setOnAction' to the same button
我从另一个 class 获取 EventHandler,我想将此 EventHandler 和其他 ActionEvent 设置为同一个按钮。有可能吗?
代码如下。
按钮是:"btnAdd".
public void addActionListener(EventHandler<ActionEvent> eventHandlerSetNotSave){
btnAdd.setOnAction((e)-> {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open Resource File");
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("pictures file format",
"*.TIF","*.JPG","*.PNG","*.GIF","*.JEPG");
fileChooser.getExtensionFilters().addAll(extFilter);
File file = fileChooser.showOpenDialog(null);
if (file != null &&
url.equals(emptyImageUrl)? true:alertMessageWarning("The viseme will change, and previous viseme will be delete.")) {
setImage("file:"+file.toString());
changeAfterSaved=false;
}
});
btnAdd.setOnAction(eventHandlerSetNotSave);
btnDelete.setOnAction((e)-> {
changeAfterSaved=true;
setImage(emptyImageUrl);
});
}
我不知道您可以用开箱即用的 JavaFX 做到这一点,但有一个简单的解决方法。您可以创建一个简单的 class 来将您的事件转发给多个处理程序,如下所示:
public class MultipleEventHandler<T extends Event> implements
EventHandler<T> {
private Collection<EventHandler<T>> handler;
public MultipleEventHandler(Collection<EventHandler<T>> handler) {
this.handler = handler;
}
@Override
public void handle(T event) {
handler.forEach(h -> h.handle(event));
}
}
然后您可以将所有事件处理程序传递给构造函数并使用此 class 作为您的事件处理程序。
MultipleEventHandler<ActionEvent> handler = new MultipleEventHandler<>(asList(e, eventHandlerSetNotSave));
btnAdd.setOnAction(handler);
调用 addEventHandler()
方法而不是 setOnAction(...)
:
btnAdd.addEventHandler(ActionEvent.ACTION, (e)-> {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open Resource File");
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("pictures file format",
"*.TIF","*.JPG","*.PNG","*.GIF","*.JEPG");
fileChooser.getExtensionFilters().addAll(extFilter);
File file = fileChooser.showOpenDialog(null);
if (file != null &&
url.equals(emptyImageUrl)? true:alertMessageWarning("The viseme will change, and previous viseme will be delete.")) {
setImage("file:"+file.toString());
changeAfterSaved=false;
}
});
btnAdd.addEventHandler(ActionEvent.ACTION, eventHandlerSetNotSave);
我从另一个 class 获取 EventHandler,我想将此 EventHandler 和其他 ActionEvent 设置为同一个按钮。有可能吗?
代码如下。 按钮是:"btnAdd".
public void addActionListener(EventHandler<ActionEvent> eventHandlerSetNotSave){
btnAdd.setOnAction((e)-> {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open Resource File");
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("pictures file format",
"*.TIF","*.JPG","*.PNG","*.GIF","*.JEPG");
fileChooser.getExtensionFilters().addAll(extFilter);
File file = fileChooser.showOpenDialog(null);
if (file != null &&
url.equals(emptyImageUrl)? true:alertMessageWarning("The viseme will change, and previous viseme will be delete.")) {
setImage("file:"+file.toString());
changeAfterSaved=false;
}
});
btnAdd.setOnAction(eventHandlerSetNotSave);
btnDelete.setOnAction((e)-> {
changeAfterSaved=true;
setImage(emptyImageUrl);
});
}
我不知道您可以用开箱即用的 JavaFX 做到这一点,但有一个简单的解决方法。您可以创建一个简单的 class 来将您的事件转发给多个处理程序,如下所示:
public class MultipleEventHandler<T extends Event> implements
EventHandler<T> {
private Collection<EventHandler<T>> handler;
public MultipleEventHandler(Collection<EventHandler<T>> handler) {
this.handler = handler;
}
@Override
public void handle(T event) {
handler.forEach(h -> h.handle(event));
}
}
然后您可以将所有事件处理程序传递给构造函数并使用此 class 作为您的事件处理程序。
MultipleEventHandler<ActionEvent> handler = new MultipleEventHandler<>(asList(e, eventHandlerSetNotSave));
btnAdd.setOnAction(handler);
调用 addEventHandler()
方法而不是 setOnAction(...)
:
btnAdd.addEventHandler(ActionEvent.ACTION, (e)-> {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open Resource File");
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("pictures file format",
"*.TIF","*.JPG","*.PNG","*.GIF","*.JEPG");
fileChooser.getExtensionFilters().addAll(extFilter);
File file = fileChooser.showOpenDialog(null);
if (file != null &&
url.equals(emptyImageUrl)? true:alertMessageWarning("The viseme will change, and previous viseme will be delete.")) {
setImage("file:"+file.toString());
changeAfterSaved=false;
}
});
btnAdd.addEventHandler(ActionEvent.ACTION, eventHandlerSetNotSave);