问题描述
我正在通过创建一个非常基本的计算器来学习如何使用javafx和scenebuilder,但是我遇到了一个问题,我很难找到一种方法来为每个按钮找不到单独的功能。我需要知道是否有一种方法可以让按钮更改变量或为按钮链接的动作事件方法提供参数,因此我不需要为每个按钮调用另一个函数的单独方法。这是我正在使用的一个例子。
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.
@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?
推荐答案
如果你在FXML注册处理程序,最好的方法是真的要为每个按钮定义一个不同的方法。您可以按如下方式最小化代码。
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.
定义 numChange(...)
方法以获取参数需要,例如
Define your numChange(...)
method to take the parameters it needs, e.g.
private void numChange(int num) {
// process num here...
}
然后为每个按钮定义一个不同的处理程序方法,只需委托给 numChange()
:
and then define a different handler method for each button that simply delegates to numChange()
:
@FXML
private void handleButtonOne() {
numChange(1);
}
@FXML
private void handleButtonTwo() {
numChange(2);
}
然后在FXML中做出明显的改变:
and then make the obvious change in the FXML:
<Button fx:id="one" onAction="#handleButtonOne" ... />
<Button fx:id="two" onAction="#handleButtonTwo" ... />
您可以更改 numChange()的参数数量和/或类型当然,
会根据需要接受。
You can alter the number and/or type of parameters that numChange()
accepts as needed, of course.
另一种基于FXML的解决方案,可避免每个按钮有一个专门的方法,就是通过设置用户数据将值与FXML中的节点相关联:
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:
<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" ... />
和
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) {
// ...
}
}
这篇关于Javafx按钮向ActionEvent函数发送参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!