java Javafx 按钮向 ActionEvent 函数发送参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37902660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 02:56:03  来源:igfitidea点击:

Javafx button sending arguments to ActionEvent function

javabuttonjavafxfxmlactionevent

提问by Foo

I'm learning how to use javafx, along with scenebuilder by creating a very basic calculator but I've come across a problem where it's hard for me to find a way to not have a seperate function for each button. I need to know if there's a way for a button to change a variable or give arguments to the action event method that the button is linked to so I don't need seperate methods for each button to call another function. This is an example of what I'm working with.

我正在通过创建一个非常基本的计算器来学习如何使用 javafx 和场景构建器,但我遇到了一个问题,我很难找到一种方法来让每个按钮都没有单独的功能。我需要知道按钮是否可以更改变量或为按钮链接的操作事件方法提供参数,因此我不需要每个按钮的单独方法来调用另一个函数。这是我正在处理的一个例子。

@FXML
private Button one;
private Button two;
//both buttons are linked to NumChange

private void NumChange(ActionEvent e) {
    //update the number to preform an operation with here
}

How do I have all the number buttons use the same function but send it arguments so it can do different things?

我如何让所有数字按钮使用相同的功能,但发送参数以便它可以做不同的事情?

All of the answers I've found involve using getting the text of the button, however my calculator is weird and I'm having the spelled out word on them so I can't do that, what can I do?

我找到的所有答案都涉及使用获取按钮的文本,但是我的计算器很奇怪,而且我在上面有拼写的单词,所以我不能这样做,我该怎么办?

回答by James_D

The best way, if you are registering the handlers in FXML, is really to define a different method for each button. You can minimize the code as follows.

如果您在 FXML 中注册处理程序,最好的方法就是为每个按钮定义不同的方法。您可以按如下方式最小化代码。

Define your numChange(...)method to take the parameters it needs, e.g.

定义你的numChange(...)方法来获取它需要的参数,例如

private void numChange(int num) {
    // process num here...
}

and then define a different handler method for each button that simply delegates to numChange():

然后为每个按钮定义一个不同的处理程序方法,该方法简单地委托给numChange()

@FXML
private void handleButtonOne() {
    numChange(1);
}

@FXML
private void handleButtonTwo() {
    numChange(2);
}

and then make the obvious change in the FXML:

然后在 FXML 中进行明显的更改:

<Button fx:id="one" onAction="#handleButtonOne" ... />
<Button fx:id="two" onAction="#handleButtonTwo" ... />

You can alter the number and/or type of parameters that numChange()accepts as needed, of course.

当然,您可以numChange()根据需要更改接受的参数的数量和/或类型。



Another FXML-based solution, which avoids each button having a dedicated method, is to associate values with nodes in FXML by setting their user data:

另一种基于 FXML 的解决方案避免了每个按钮都具有专用方法,是通过设置用户数据将值与 FXML 中的节点相关联:

<Button fx:id="one" onAction="#numChange" userData="1" ... />
<Button fx:id="two" onAction="#numChange" userData="2" ... />

However this just results in a lot of downcasting and type conversion, which imho makes it less useful than the previous solution:

然而,这只会导致大量的向下转换和类型转换,恕我直言,这使它不如以前的解决方案有用:

@FXML
private void numChange(ActionEvent event) {
    Node node = (Node) event.getSource() ;
    String data = (String) node.getUserData();
    int value = Integer.parseInt(data);

    // ...
}


In the end, it might just be easier to register the handlers directly in the controller:

最后,直接在控制器中注册处理程序可能更容易:

<Button fx:id="one" ... />
<Button fx:id="two" ... />

and

public class Controller {

    @FXML
    private Button one ;

    @FXML
    private Button two ;

    public void initialize() {
        one.setOnAction(e -> numChange(1));
        two.setOnAction(e -> numChange(2));
    }

    private void numChange(int value) {
        // ...
    }
}

回答by user3502626

Just implement EventHandler<ActionEvent>or create an EventHandler<ActionEvent>anonyme class in your class and call on each button you want setOnAction(myAction);

只需在您的班级中实现EventHandler<ActionEvent>或创建一个EventHandler<ActionEvent>匿名类并调用您想要的每个按钮setOnAction(myAction);