使用 setOnAction 添加现有的处理程序方法
addign a existing handler method with setOnAction
我想将现有的 handleModellAction
方法分配给使用 setOnAction
方法生成的超链接,但我不知道该怎么做。
这是我的代码示例:-
@FXML
private void handleModellAction(ActionEvent event) throws IOException{
FXMLLoader load = new FXMLLoader(getClass().getResource("InEX.fxml"));
Parent root = (Parent) load.load();
Stage stage = new Stage();
stage.setScene(new Scene(root));
stage.show();
link = (Hyperlink) event.getTarget();
model = link.getId();
stage.setTitle(model);
}
public void addNeuesModell(String bauart, String modelName){
modelHyperlink = new Hyperlink();
modelHyperlink.setId(modelName);
modelHyperlink.setText(modelName);
modelHyperlink.setOnAction(#handleModellAction);
}
有人知道怎么做吗?
非常感谢:)
您可以尝试调用 modelHyperlink
上的 setOnAction
方法,并将匿名 class 作为参数作为处理程序传递,您可以在其中传输 handleModellAction
方法。您可以在下面找到一个示例:
Hyperlink link = new Hyperlink();
link.setText("http://example.com");
link.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
FXMLLoader load = new
FXMLLoader(getClass().getResource("InEX.fxml"));
Parent root = (Parent) load.load();
Stage stage = new Stage();
stage.setScene(new Scene(root));
stage.show();
link = (Hyperlink) event.getTarget();
model = link.getId();
stage.setTitle(model);
}
});
而不是定义
<HyperLink fx:id="myLink" onAction="#handleModelAction"/>
您只能使用:
<HyperLink fx:id="myLink"/>
然后在代码中:
像这样重构你的 handleMethod:
private void handleModellAction() throws IOException {
FXMLLoader load = new FXMLLoader(getClass().getResource("InEX.fxml"));
Parent root = load.load();
Stage stage = new Stage();
stage.setScene(new Scene(root));
stage.show();
}
之后:
myLink.setOnAction(action -> {
try {
handleModellAction();
} catch (IOException e) {
e.printStackTrace();
}
});
然后您可以在任何按钮、超链接等任何地方使用 handleModellAction()
..
我想将现有的 handleModellAction
方法分配给使用 setOnAction
方法生成的超链接,但我不知道该怎么做。
这是我的代码示例:-
@FXML
private void handleModellAction(ActionEvent event) throws IOException{
FXMLLoader load = new FXMLLoader(getClass().getResource("InEX.fxml"));
Parent root = (Parent) load.load();
Stage stage = new Stage();
stage.setScene(new Scene(root));
stage.show();
link = (Hyperlink) event.getTarget();
model = link.getId();
stage.setTitle(model);
}
public void addNeuesModell(String bauart, String modelName){
modelHyperlink = new Hyperlink();
modelHyperlink.setId(modelName);
modelHyperlink.setText(modelName);
modelHyperlink.setOnAction(#handleModellAction);
}
有人知道怎么做吗?
非常感谢:)
您可以尝试调用 modelHyperlink
上的 setOnAction
方法,并将匿名 class 作为参数作为处理程序传递,您可以在其中传输 handleModellAction
方法。您可以在下面找到一个示例:
Hyperlink link = new Hyperlink();
link.setText("http://example.com");
link.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
FXMLLoader load = new
FXMLLoader(getClass().getResource("InEX.fxml"));
Parent root = (Parent) load.load();
Stage stage = new Stage();
stage.setScene(new Scene(root));
stage.show();
link = (Hyperlink) event.getTarget();
model = link.getId();
stage.setTitle(model);
}
});
而不是定义
<HyperLink fx:id="myLink" onAction="#handleModelAction"/>
您只能使用:
<HyperLink fx:id="myLink"/>
然后在代码中: 像这样重构你的 handleMethod:
private void handleModellAction() throws IOException {
FXMLLoader load = new FXMLLoader(getClass().getResource("InEX.fxml"));
Parent root = load.load();
Stage stage = new Stage();
stage.setScene(new Scene(root));
stage.show();
}
之后:
myLink.setOnAction(action -> {
try {
handleModellAction();
} catch (IOException e) {
e.printStackTrace();
}
});
然后您可以在任何按钮、超链接等任何地方使用 handleModellAction()
..