简单计算器(GUI)导致异常

时间:2018-08-14 01:52:54

标签: java javafx

我正在尝试用图形用户界面制作一个简单的计算器;但是,我在从文本字段添加两个值时遇到麻烦。每次尝试计算值时,都会收到一个异常。有人可以帮我这个忙吗?我对Java还是很陌生,并且愿意接受反馈和建议。异常在下面。

谢谢

凯尔

query_data = pymongo.db.mycollection.find({
   condition1
})

result = query_data.where(Code("function() {
  return condition2}"))

Simple Calculator Picture

public class Calculator extends Application 
{
private Label firstValue;
private Label secondValue;
private Label sum;
private Button myButton;
private TextField textSum;
private TextField textFirst;
private TextField textSecond;

public String calculation()
{
    String getTextFirst = textFirst.getText();
    String getTextSecond = textSecond.getText();

    int total = Integer.parseInt(getTextFirst) + 
    Integer.parseInt(getTextSecond);
    String realTotal = String.valueOf(total);
    return realTotal;
}

public void start(Stage myStage)
{

    myStage.setTitle("Simple Calculator");
    GridPane rootNode = new GridPane();
    rootNode.setAlignment(Pos.CENTER);
    rootNode.setPadding(new Insets(30));
    Scene myScene = new Scene (rootNode, 400 , 300);
    firstValue = new Label ("First Value: ");
    secondValue = new Label ("Second Value: ");
    sum = new Label ("Sum is: ");

    textFirst = new TextField();
    textSecond = new TextField();
    textSum = new TextField();

    textSum.setEditable(false);
    myButton = new Button ("Calculate");

    rootNode.add(firstValue, 1, 0);
    rootNode.add(textFirst, 2, 0);
    rootNode.add(secondValue, 1, 1);
    rootNode.add(textSecond, 2, 1);
    rootNode.add(sum, 1, 2);
    rootNode.add(textSum, 2, 2);
    rootNode.add(myButton, 2, 3);

    myButton.setOnAction(new ButtonHandler());


    myStage.setScene(myScene);
    myStage.show();



}

class ButtonHandler implements EventHandler<ActionEvent>
{
    public void handle(ActionEvent e)
    {
        Calculator finalTotal = new Calculator();

        myButton.setText("Calculating");

        textSum.setText(finalTotal.calculation());
    }
}


public static void main(String[] args)
{
    launch(args);
}
}

1 个答案:

答案 0 :(得分:0)

首先,您的ButtonHandler创建一个Calculator的新实例,它与当前显示在屏幕上的Calculator的实例无关

class ButtonHandler implements EventHandler<ActionEvent>
{
    public void handle(ActionEvent e)
    {
        Calculator finalTotal = new Calculator();

        myButton.setText("Calculating");

        textSum.setText(finalTotal.calculation());
    }
}

这意味着当调用calculation时...

public String calculation()
{
    String getTextFirst = textFirst.getText();
    String getTextSecond = textSecond.getText();

    int total = Integer.parseInt(getTextFirst) + Integer.parseInt(getTextSecond);
    String realTotal = String.valueOf(total);
    return realTotal;
}

由于您的UI组件是通过start方法创建的,因此在创建第二个实例时永远不会调用它,因此textFirsttextSecond均为null

首先修改您的处理程序以引用Calculator

class ButtonHandler implements EventHandler<ActionEvent>
{
    private Calculator calculator;

    public ButtonHandler(Calculator calculator) {
        this.calculator = calculator
    }

    public void handle(ActionEvent e)
    {
        this.calculator.calculation();
    }
}

采取这种措施后,您可以简单地实际在屏幕上的calculation实例的所有Calculator

然后您将需要更新处理程序的创建...

myButton.setOnAction(new ButtonHandler(this));

最后,我将更新calculation来更新UI,因为字段为private ...

public void calculation()
{
    String getTextFirst = textFirst.getText();
    String getTextSecond = textSecond.getText();

    int total = Integer.parseInt(getTextFirst) + Integer.parseInt(getTextSecond);
    String realTotal = String.valueOf(total);

    textSum.setText(realTotal);
}