gpt4 book ai didi

Java FX 将函数作为参数发送到方法

转载 作者:行者123 更新时间:2023-12-02 09:54:08 25 4
gpt4 key购买 nike

更新 2019.05.14 4:54PM EST - 好的 - 这是说明我的问题的代码 - 可能花了我太长时间才得到这个,而且可能太长了,但同样,我是 Java 新手。无论如何 - 它有效,打开表单,但无法让第二个按钮看到“事件”并对“事件”使用react。我想我现在正在“引发”事件,好吧 - 至少它击中了事件中的代码。但是,它仍然运行类代码,而不是通过 setOnFormStateChange 传入的“自定义”代码???我不确定出了什么问题。

所有导入

import java.util.ArrayList;
import java.util.Date;
import javafx.application.Application;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

MyApp.java

public class MyApp extends Application {

public static void main(String[] args) {
launch(args); // this method will hang here until main form is closed!
}

@Override
public void start(Stage stage) throws Exception {
Form myForm = new Form();
myForm.ShowForm();
}

}

Form.java

class Form {

private boolean modified;
private ArrayList<FormStateChangeListener> registry = new ArrayList<>();

public void setModified(boolean m) {
modified = m;
this.throwStateChange(m);
}

public void throwStateChange(boolean m) {
for (FormStateChangeListener o : registry) {
// o.onFormStateChange(m);
FormStateChangeEvent.fireEvent(o,m);
}
}

public void ShowForm() {

Label lbl = new Label("NORMAL STATE");
lbl.setLayoutX(50);
lbl.setLayoutY(20);

Btn myBtn1 = new Btn(this);
myBtn1.setLayoutX(100);
myBtn1.setLayoutY(100);
myBtn1.setPrefWidth(200);

myBtn1.setText("Press To Change State");
myBtn1.setOnAction(e -> {
lbl.setText("CHANGED STATE:" + new Date().toString());
this.setModified(true);
});

Btn myBtn2 = new Btn(this);
myBtn2.setLayoutX(100);
myBtn2.setLayoutY(200);
myBtn2.setPrefWidth(200);
myBtn2.setText("And This Should React");

myBtn2.setOnFormStateChange(e -> {
myBtn2.setText("I REACTED!");
});


Stage stage = new Stage();
AnchorPane root = new AnchorPane();
Scene scene = new Scene(root, 430, 400);
// root.getChildren().add(lbl);
// root.getChildren().add(myBtn1);
// root.getChildren().add(myBtn2);
root.getChildren().addAll(lbl,myBtn1,myBtn2);
stage.setScene(scene);
stage.showAndWait();
}

void registerForEvent(FormStateChangeListener t) {
registry.add(t);

}

}

FormStateChangeListener.java

interface FormStateChangeListener {
public void onFormStateChange(boolean mod);
}

FormState.java

enum FormState {
NORMAL, MODIFIED, NEW
}

Btn.java

class Btn extends Button implements FormStateChangeListener {

private final ObjectProperty<EventHandler<? super FormStateChangeEvent>> onFormStateChange
= new SimpleObjectProperty<EventHandler<? super FormStateChangeEvent>>(this, "onFormStateChange") {

@Override
protected void invalidated() {
setEventHandler(FormStateChangeEvent.FORM_STATE_CHANGE, get());
}
};

public Btn(Form f) { // constructor
f.registerForEvent(this); // register for the event
}

public final void setOnFormStateChange(EventHandler<? super FormStateChangeEvent> handler) {
onFormStateChange.set(handler);
}

public final EventHandler<? super FormStateChangeEvent> getOnFormStateChange() {
return onFormStateChange.get();
}

public final ObjectProperty<EventHandler<? super FormStateChangeEvent>> onFormStateChangeProperty() {
return onFormStateChange;
}

public void onFormStateChange(boolean mod) {
//in reality nothing would be here, but is just for testing
System.out.println("Code from class.");
}
;

}

FormStateChangeEvent.java

class FormStateChangeEvent extends Event {

public static final EventType<FormStateChangeEvent> ANY = new EventType<>(Event.ANY, "MY_EVENT");
public static final EventType<FormStateChangeEvent> FORM_STATE_CHANGE = new EventType<>(ANY, "FORM_STATE_CHANGE");

static void fireEvent(FormStateChangeListener o, boolean mod) {
//throw new UnsupportedOperationException("Not supported yet.");
o.onFormStateChange(mod);
}

public FormStateChangeEvent(EventType<? extends FormStateChangeEvent> eventType) {
super(eventType);
}
}

最佳答案

诸如setOnMouseClicked之类的方法接受javafx.event.EventHandler,它是一个函数式接口(interface)。换句话说,它有一个抽象方法,这意味着它可以用作 lambda 表达式或方法引用的目标。如果您的目标是拥有自己的具有相应 onXXX 属性的 Event 类,那么您的 setOnXXX 方法必须接受 EventHandler 像所有其他人一样。

但是,如果您希望在正常事件分派(dispatch)期间调用 EventHandler,仅仅设置该属性是不够的。您必须向事件调度程序注册它,这可以通过 protected Node.setEventHandler(EventType,EventHandler) 方法来完成。

例如,假设以下是您的事件类:

import javafx.event.Event;
import javafx.event.EventType;

public class MyEvent extends Event {

public static final EventType<MyEvent> ANY = new EventType<>(Event.ANY, "MY_EVENT");
public static final EventType<MyEvent> AWESOME_THING = new EventType<>(ANY, "AWESOME_THING");

public MyEvent(EventType<? extends MyEvent> eventType) {
super(eventType);
}

}

那么您的自定义节点将类似于:

import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.event.EventHandler;
import javafx.scene.control.Button;

public class MyButton extends Button {

private final ObjectProperty<EventHandler<? super MyEvent>> onAwesomeThing
= new SimpleObjectProperty<>(this, "onAwesomeThing") {
@Override protected void invalidated() {
setEventHandler(MyEvent.AWESOME_THING, get());
}
};

public final void setOnAwesomeThing(EventHandler<? super MyEvent> handler) {
onAwesomeThing.set(handler);
}

public final EventHandler<? super MyEvent> getOnAwesomeThing() {
return onAwesomeThing.get();
}

public final ObjectProperty<EventHandler<? super MyEvent>> onAwesomeThingProperty() {
return onAwesomeThing;
}

}

通常,与事件关联的每个 EventType 都有一个属性,“常规”事件类型(例如 MyEvent.ANY)除外。

现在您可以调用 myBtn.fireEvent(new MyEvent(MyEvent.AWESOME_THING)) 并且您注册的 EventHandler 将被调用。您还可以通过 addEventFilteraddEventHandler 注册处理程序。

关于Java FX 将函数作为参数发送到方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56121354/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com