UPDATE EST 2019.05.14 4:54 PM EST-好的-这是说明我的问题的代码-可能花了我很长时间才能获得此代码,而且可能太长了,但是我还是Java的新手。无论如何,它可以工作,显示表格,并且无法让第二个按钮看到“事件”并对事件做出反应。我认为我现在正在“引发”事件,确定-至少它正在击中事件中的代码。但是,它仍在运行类代码,而不是通过setOnFormStateChange传递的“自定义”代码?我不知道怎么了。
所有进口
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<outputDirectory>${basedir}</outputDirectory>
<finalName>${project.artifactId}</finalName>
</configuration>
</execution>
</executions>
MyApp.java
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;
Form.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();
}
}
FormStateChangeListener.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);
}
}
FormState.java
interface FormStateChangeListener {
public void onFormStateChange(boolean mod);
}
Btn.java
enum FormState {
NORMAL, MODIFIED, NEW
}
FormStateChangeEvent.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.");
}
;
}
答案 0 :(得分:1)
诸如<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p class="item">
<span class="input--span"></span>
<input class="input--item" type="text" name="name" value="Item 1 Long Text" autocomplete="off" />
</p>
<p class="item">
<span class="input--span"></span>
<input class="input--item" type="text" name="name" value="Item 2" autocomplete="off" />
</p>
<p class="item">
<span class="input--span"></span>
<input class="input--item" type="text" name="name" value="I3" autocomplete="off" />
</p>
之类的方法接受STRING_SPLIT()
,这是一个功能接口。换句话说,它具有单个抽象方法,这意味着它可用作lambda表达式或方法引用的目标。如果您的目标是拥有自己的setOnMouseClicked
类和相应的javafx.event.EventHandler
属性,那么您的Event
方法必须像其他所有方法一样接受onXXX
。
但是,如果您希望在常规事件分派期间调用setOnXXX
,仅设置属性是不够的。您必须在事件分发程序中注册它,这可以通过受保护的EventHandler
方法来完成。
例如,假设以下是您的事件类:
EventHandler
然后,您的自定义节点将类似于:
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
注册处理程序。