Lambda在JavaFX8中使用FXML [英] Lambda functions with FXML in JavaFX8

查看:242
本文介绍了Lambda在JavaFX8中使用FXML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Scene Builder在JavaFX中使用入侵者游戏(我必须使用加农炮拍摄飞机)但是我遇到了lambda函数的问题。

I'm working to an invader game in JavaFX with Scene Builder (I have to shoot planes whit a cannon) but I have got problems with lambda function.

我确定代码是正确的,因为如果我通过Scene Builder的选项(On action)关联代码它可以工作,但是,当我尝试使用lambda函数时,我无法改变我的场景。我哪里错了?

I'm sure code is right because if I associate the code through Scene Builder's option (On action) it works, but, when I try to use lambda function, I can't change my scene. Where am I wrong?

public class SchermataGiocoController {

private Parent Menu, Avvio;
private TranslateTransition tt;
private Cannone cannone; //cannone = cannon
private Aereo aereo; //aereo = plane
private Proiettile proiettile; //proiettile = bullet
private RotateTransition rt;

@FXML
private Button down;

@FXML
private Circle circle;

@FXML
private Button su;

@FXML
private Button exit;

@FXML
private ImageView cannone_im;

@FXML
private AnchorPane SchermataGioco;

@FXML
private ImageView aereo_im;

@FXML
private Button menu;

@FXML
private Button home;

@FXML
void initialize() {
    assert exit != null : "fx:id=\"exit\" was not injected: check your FXML file 'SchermataGioco.fxml'.";
    assert cannone_im != null : "fx:id=\"cannone_im\" was not injected: check your FXML file 'SchermataGioco.fxml'.";
    assert su != null : "fx:id=\"su\" was not injected: check your FXML file 'SchermataGioco.fxml'.";
    assert SchermataGioco != null : "fx:id=\"SchermataGioco\" was not injected: check your FXML file 'SchermataGioco.fxml'.";
    assert aereo_im != null : "fx:id=\"aereo_im\" was not injected: check your FXML file 'SchermataGioco.fxml'.";
    assert menu != null : "fx:id=\"menu\" was not injected: check your FXML file 'SchermataGioco.fxml'.";
    assert down != null : "fx:id=\"down\" was not injected: check your FXML file 'SchermataGioco.fxml'.";
    assert home != null : "fx:id=\"home\" was not injected: check your FXML file 'SchermataGioco.fxml'.";
    cannone = new Cannone(141, 104, 14, 362);
    proiettile = new Proiettile(16, 16, 14, 362); 
    TranslateTransition();
}


 @FXML
 public void vaiMenu() {
    menu.setOnAction((ActionEvent event) -> {                      
        try {                
            FXMLLoader loader = new FXMLLoader();                
            loader.setLocation(Game.class.getResource("/game/view/Menu.fxml"));
            Menu = (Parent) loader.load();
            menu.getScene().getWindow().hide();
        } catch (IOException ioe) {
          ioe.getMessage();
        }            
        Stage stage = new Stage();
        stage.setScene(new Scene(Menu));
        stage.show();
    });
}


推荐答案

如果您关联 vaiMenu() SceneBuilder 中使用On Action,然后(如预期的那样),例如当在按钮上执行单击时,将执行 vaiMenu()方法,该方法将执行:assign 按钮的动作事件的监听器(通过你的lambda),因此它永远不会被执行,只是一次又一次地添加。

If you associate vaiMenu() with the "On Action" in SceneBuilder, then (as it is expected), on e.g. when a click is performed on the Button, the vaiMenu() method will be executed, which will just do: assign a listener (through your lambda) for the action event of the Button, therefore it never will be executed, just added again-and-again.

如果你想通过 SceneBuilder 分配监听器,如:

If you want to assign the listener through SceneBuilder like:

<Button fx:id="menu" onAction="#vaiMenu" />

你不能使用lambdas(匿名函数),因为你需要在你的FXML文件中引用一个命名函数(在 onAction 属性中)。

you cannot use lambdas (anonymous functions) as you need a named function to be referenced in your FXML file (in the onAction attribute).

如果您将当前的lambda插入例如控制器的 initialize()方法,它可以正常工作,但在这种情况下,你不应该在FXML文件中定义这个属性。

If you insert your current lambda in e.g. the initialize() method of your controller, it will work without any problems, but in this case you should not define this attribute in your FXML file.

这篇关于Lambda在JavaFX8中使用FXML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆