英文:
Javafx Addition
问题
我最近开始使用JavaFX,对它非常陌生。我正在尝试创建一个程序,在你输入所有代码并运行后,会出现一个小框,要求你输入数字1和数字2,然后程序将这两个数字相加并在结果选项卡中打印结果。我卡在了需要编写代码以便你按按钮计算数字和需要添加这两个数字的地方。
谢谢
import java.math.BigInteger;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.event.ActionEvent;
import javafx.scene.control.TextField;
public class AdditionAppController {
@FXML
private Label Number1Label;
@FXML
private Label Number2Label;
@FXML
private Label ResultLabel;
@FXML
private TextField Number1TextField;
@FXML
private TextField Number2TextField;
@FXML
private TextField ResultTextField;
@FXML
private Button CalculateButtonPressed;
@FXML
void CalculateButtonPressed(ActionEvent event) {
try {
BigInteger Number1 = new BigInteger(Number1TextField.getText());
BigInteger Number2 = new BigInteger(Number2TextField.getText());
BigInteger result = Number1.add(Number2);
//ResultTextField.setText(Integer.(result));
} catch (NumberFormatException ex) {
Number1TextField.setText("输入第一个数字");
Number1TextField.selectAll();
Number1TextField.requestFocus();
Number2TextField.setText("输入第二个数字");
Number2TextField.selectAll();
Number2TextField.requestFocus();
}
}
}
英文:
I recently started using Javafx and I'm very new to it I am trying a create a program where once you enter all the code and run it a small box appears asking you to enter number 1 and number 2 and then the program sums up both of those numbers and print the result in the result tab. I am getting stuck on where you have to write a code in order for you to press the button to calculate the number and where you need to add both of number.
Thanks
import java.math.BigInteger;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.event.ActionEvent;
import javafx.scene.control.TextField;
public class AdditionAppController {
@FXML
private Label Number1Label;
@FXML
private Label Number2Label;
@FXML
private Label ResultLabel;
@FXML
private TextField Number1TextField;
@FXML
private TextField Number2TextField;
@FXML
private TextField ResultTextField;
@FXML
private Button CalculateButtonPressed;
@FXML
void CalculateButtonPressed(ActionEvent event) {
try {
BigInteger Number1 = new BigInteger(Number1TextField.getText());
BigInteger Number2 = new BigInteger(Number2TextField.getText());
BigInteger result = Number1.add(Number2);
//ResultTextField.setText(Integer.(result));
} catch (NumberFormatException ex) {
Number1TextField.setText("Enter first number");
Number1TextField.selectAll();
Number1TextField.requestFocus();
Number2TextField.setText("Enter second number");
Number2TextField.selectAll();
Number2TextField.requestFocus();
}
}
}
答案1
得分: 1
你需要为你的CalculateButtonPressed添加一个事件处理程序。
如果你正在使用Scene Builder,你需要点击“Calculate Button”,然后在右侧收起所有其他部分,展开“Code”部分。
所以在fx:id中,你需要将ID设置为CalculateButtonPressed
,而“OnAction”设置为类似于onCalculateButtonPressed
的内容。
然后回到你的代码中,从FXML添加一个事件处理程序,就像这样:
@FXML
void onCalculateButtonPressed(ActionEvent event) {
//在这里添加计算总和的代码
}
Scene Builder的一个有趣功能是,如果你转到“View” > “Show Sample Controller Skeleton”,它会为你生成一些基本代码。你可以将其中的部分(或全部)复制粘贴到你自己的控制器中。
如果你不使用SceneBuilder,在你的fxml中为组件添加fx:id=""
和onAction="#actionHere"
,像这样:
<Button fx:id="CalculateButtonPressed" onAction="#onCalculateButtonPressed" text=... />
然后你仍然需要添加我上面发布的相同的事件处理程序代码。
英文:
You need to add an event handler to your CalculateButtonPressed.
If you're using Scene Builder, what you would do is click on the "Calculate Button", and on the right hand side collapse all the other sections and expand the "Code" section.
so in fx:id you would set the ID to CalculateButtonPressed
, and "OnAction" to something like "onCalculateButtonPressed".
Then go back to your code and add an event handler from FXML like so:
@FXML
void onCalculateButtonPressed(ActionEvent event) {
//Add your code to calculate your sum here
}
Fun feature of Scene Builder, you can copy some skeleton code if you go to View
> Show Sample Controller Skeleton
and it will generate it for you. You can copy and paste parts of it (or all of it) into your own controller.
If you're NOT using SceneBuilder, in your fxml add to the component fx:id=""
and onAction="#actionHere"
so yours would be:
<Button fx:id="CalculateButtonPressed" onAction="#onCalculateButtonPressed" text=... />
And you would still add the same event handler code that I posted above.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论