java - 导致异常的简单计算器 (GUI)
问题描述
我正在尝试制作一个带有图形用户界面的简单计算器;但是,我无法从文本字段中添加两个值。每次我尝试计算这些值时,我都会收到一个异常。有人可以帮我解决这个问题吗?我对Java相当陌生,我愿意接受反馈和建议。例外在下面。
谢谢,
凯尔
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);
}
}
Exception in thread "JavaFX Application Thread"
java.lang.NullPointerException
at Calculator.calculation(Calculator.java:22)
at Calculator$ButtonHandler.handle(Calculator.java:75)
at Calculator$ButtonHandler.handle(Calculator.java:1)
解决方案
首先,您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
因此两者都是textFirst
textSecond
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);
}
推荐阅读
- python - 如何使用滚动条创建固定宽度的 tkinter Treeview?
- xml - 链接实体导致异常,不知道为什么?
- python - python如何加快查询海量数据的速度?
- javascript - 新的 VueJS 项目 - eslint-loader - TypeError: eslint.CLIEngine 不是构造函数
- python - 尝试使用 VSC 调试远程部署的容器化 Django 应用程序时出现“未找到模块”
- python - 从现有容器创建新图像但未复制数据?
- node.js - 错误:不支持选项 gssapiservicename
- android - Cordova 10 不接受引擎标签
- angular - 如果指令属性为真,则对需要添加 css 类的指令进行单元测试
- flutter - 如何在使用颤振相机插件录制视频时选择音频源