JavaFX Addition

huangapple go评论97阅读模式
英文:

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”部分。

JavaFX Addition

所以在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.

JavaFX Addition

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=&quot;&quot; and onAction=&quot;#actionHere&quot; so yours would be:

&lt;Button fx:id=&quot;CalculateButtonPressed&quot; onAction=&quot;#onCalculateButtonPressed&quot; text=... /&gt;

And you would still add the same event handler code that I posted above.

huangapple
  • 本文由 发表于 2020年4月8日 22:27:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/61103124.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定