action在java,在方法java中的action上设置按钮

How could I set the action of a button within the same method I am creating the buttons?

My desired method would be something like this:

private void buttonsCreation() {

//----------------creation of interactive buttons with text-----------------

Button buttonForLoad = new Button("Load footage file");

Button buttonForSave = new Button("Save footage file");

Button buttonForSaveAs = new Button("Save as footage file");

ButtonbuttonForRun = new Button("Run footage animation");

Button buttonForTerminate = new Button("Stop footage animation");

Button buttonForEditMenu = new Button("Edit current footage");

//---------------setting the interaction of the buttons---------------------

buttonForLoad.setOnAction(loadFootage());

buttonForSave.setOnAction(saveFootage());

buttonForSaveAs.setOnAction(saveAs());

buttonForRun.setOnAction(runAnimation());

buttonForTerminate.setOnAction(terminateAnimatino());

buttonForEditMenu.setOnAction(editMenu());

}

I would like the attributes of setOnAction to call those methods, but I receive this error. setOnAction in ButonBase can not be applied to void.

I am aware I can create a void handle with an ActionEvent as a parameter and make it work, but my desired function will be in one function, and if it is possible with as least lines of code as possible.

Thanks very much

解决方案

To call void function in action handler, lambda expression is usefull. Like this:

buttonForLoad.setOnAction(e -> loadFootage());

buttonForSave.setOnAction(e -> saveFootage());

...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值