在方法java中的action上设置按钮 [英] set button on action within method java
问题描述
如何用相同的方法设置按钮的动作?我正在创建按钮?
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());
}
我希望setOnAction的属性调用这些方法,但是我收到此错误. ButonBase中的setOnAction不能应用于void.
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.
我知道我可以使用ActionEvent作为参数来创建一个空句柄并使它工作,但是我想要的函数将在一个函数中,并且如果可能的话,请使用尽可能少的代码行.
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.
非常感谢
推荐答案
要在动作处理程序中调用void函数,lambda表达式很有用.像这样:
To call void function in action handler, lambda expression is usefull. Like this:
buttonForLoad.setOnAction(e -> loadFootage());
buttonForSave.setOnAction(e -> saveFootage());
...
这篇关于在方法java中的action上设置按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!