Java 使用多个文本字段正确处理异常
节目图像: 这是由“+”按钮调用的方法的代码 目前,我很难正确地找出哪个textField抛出异常。 上面的代码不起作用,因为未初始化双精度值。 但如果tf_X值不是一个数字,我不想仅仅用0来计算 我还尝试让Java 使用多个文本字段正确处理异常,java,swing,exception,exception-handling,jtextfield,Java,Swing,Exception,Exception Handling,Jtextfield,节目图像: 这是由“+”按钮调用的方法的代码 目前,我很难正确地找出哪个textField抛出异常。 上面的代码不起作用,因为未初始化双精度值。 但如果tf_X值不是一个数字,我不想仅仅用0来计算 我还尝试让addoperans()返回一个双精度但 出现异常时,无法返回null 我想要的是: 如果任何值不是数字,则不计算结果(引发NumberFormatException) 将包含NaN值的文本字段的背景设置为红色 清除tf_3的当前值:tf_3.setText(“”) 有人能帮我吗?在您
addoperans()
返回一个双精度但
出现异常时,无法返回null
我想要的是:
- 如果任何值不是数字,则不计算结果(引发NumberFormatException)
- 将包含NaN值的文本字段的背景设置为红色
- 清除tf_3的当前值:
tf_3.setText(“”)代码>
有人能帮我吗?在您的
添加操作数的代码中
private void addOperands() {
double value_1, value_2;
try {
value_1 = Double.parseDouble(tf_1.getText());
} catch (NumberFormatException e) {
tf_1.setBackground(Color.red);
tf_1.requestFocus();
}
try {
value_2 = Double.parseDouble(tf_2.getText());
} catch (Exception e) {
tf_2.setBackground(Color.red);
tf_2.requestFocus();
}
tf_3.setText(String.valueOf(value_1 + value_2));
}
当您在第一个catch
try {
value_1 = Double.parseDouble(tf_1.getText());
} catch (NumberFormatException e) {
// here you know that tf_1 is non-double
tf_1.setBackground(Color.red);
tf_1.requestFocus();
}
您知道第一个输入包含错误的输入。第二次输入也是如此。如果不希望结果显示0,则有2种选择:在第一个错误时停止处理,只需将return
放在catch
子句中,或者添加标志以查看是否一切正常
private void addOperands() {
double value_1, value_2;
boolean good = true;
try {
value_1 = Double.parseDouble(tf_1.getText());
} catch (NumberFormatException e) {
good = false;
tf_1.setBackground(Color.red);
tf_1.requestFocus();
}
try {
value_2 = Double.parseDouble(tf_2.getText());
} catch (Exception e) {
good = false;
tf_2.setBackground(Color.red);
tf_2.requestFocus();
}
if(good)
tf_3.setText(String.valueOf(value_1 + value_2));
else
tf_3.setText("Bad input");
}
更新
如果你想分解你的代码(通常这是个好主意),你可以这样做
private Double readDoubleOrHighlight(JTextField tf) {
try {
double res = Double.parseDouble(tf.getText());
tf.setBackground(whatever-default-background-is);
return res;
} catch (NumberFormatException e) {
tf.setBackground(Color.red);
tf.requestFocus();
return null;
}
}
private void addOperands() {
Double value_1 = readDoubleOrHighlight(tf_1);
Double value_2 = readDoubleOrHighlight(tf_2);
if((value_1 != null) && (value_2 != null))
tf_3.setText(String.valueOf(value_1 + value_2));
else
tf_3.setText("Bad input");
}
static interface BinaryOp {
double calc(double arg1, double arg2);
}
private void applyBinaryOp(BinaryOp op) {
Double value_1 = readDoubleOrHighlight(tf_1);
Double value_2 = readDoubleOrHighlight(tf_2);
if((value_1 != null) && (value_2 != null)) {
double res = op.calc(value_1, value_2);
tf_3.setText(res);
}
else
tf_3.setText("Bad input");
}
bu_2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
applyBinaryOp(new BinaryOp(){
public double calc(double arg1, double arg2) {
return arg1 * arg2;
}
});
}
});
注意:这里我有效地使用了
Double
作为选项[Double]
,这在Java中是不受支持的(提示:googleOption
,这是一个强大的想法或从开始)
更新#2
这一次引入了高阶函数。
如果您添加这样一段代码
private Double readDoubleOrHighlight(JTextField tf) {
try {
double res = Double.parseDouble(tf.getText());
tf.setBackground(whatever-default-background-is);
return res;
} catch (NumberFormatException e) {
tf.setBackground(Color.red);
tf.requestFocus();
return null;
}
}
private void addOperands() {
Double value_1 = readDoubleOrHighlight(tf_1);
Double value_2 = readDoubleOrHighlight(tf_2);
if((value_1 != null) && (value_2 != null))
tf_3.setText(String.valueOf(value_1 + value_2));
else
tf_3.setText("Bad input");
}
static interface BinaryOp {
double calc(double arg1, double arg2);
}
private void applyBinaryOp(BinaryOp op) {
Double value_1 = readDoubleOrHighlight(tf_1);
Double value_2 = readDoubleOrHighlight(tf_2);
if((value_1 != null) && (value_2 != null)) {
double res = op.calc(value_1, value_2);
tf_3.setText(res);
}
else
tf_3.setText("Bad input");
}
bu_2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
applyBinaryOp(new BinaryOp(){
public double calc(double arg1, double arg2) {
return arg1 * arg2;
}
});
}
});
您可以将单击列表简化为以下内容
private Double readDoubleOrHighlight(JTextField tf) {
try {
double res = Double.parseDouble(tf.getText());
tf.setBackground(whatever-default-background-is);
return res;
} catch (NumberFormatException e) {
tf.setBackground(Color.red);
tf.requestFocus();
return null;
}
}
private void addOperands() {
Double value_1 = readDoubleOrHighlight(tf_1);
Double value_2 = readDoubleOrHighlight(tf_2);
if((value_1 != null) && (value_2 != null))
tf_3.setText(String.valueOf(value_1 + value_2));
else
tf_3.setText("Bad input");
}
static interface BinaryOp {
double calc(double arg1, double arg2);
}
private void applyBinaryOp(BinaryOp op) {
Double value_1 = readDoubleOrHighlight(tf_1);
Double value_2 = readDoubleOrHighlight(tf_2);
if((value_1 != null) && (value_2 != null)) {
double res = op.calc(value_1, value_2);
tf_3.setText(res);
}
else
tf_3.setText("Bad input");
}
bu_2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
applyBinaryOp(new BinaryOp(){
public double calc(double arg1, double arg2) {
return arg1 * arg2;
}
});
}
});
Java 8 lambda语法更简单。在您的
添加操作数的代码中
private void addOperands() {
double value_1, value_2;
try {
value_1 = Double.parseDouble(tf_1.getText());
} catch (NumberFormatException e) {
tf_1.setBackground(Color.red);
tf_1.requestFocus();
}
try {
value_2 = Double.parseDouble(tf_2.getText());
} catch (Exception e) {
tf_2.setBackground(Color.red);
tf_2.requestFocus();
}
tf_3.setText(String.valueOf(value_1 + value_2));
}
当您在第一个catch
try {
value_1 = Double.parseDouble(tf_1.getText());
} catch (NumberFormatException e) {
// here you know that tf_1 is non-double
tf_1.setBackground(Color.red);
tf_1.requestFocus();
}
您知道第一个输入包含错误的输入。第二次输入也是如此。如果不希望结果显示0,则有2种选择:在第一个错误时停止处理,只需将return
放在catch
子句中,或者添加标志以查看是否一切正常
private void addOperands() {
double value_1, value_2;
boolean good = true;
try {
value_1 = Double.parseDouble(tf_1.getText());
} catch (NumberFormatException e) {
good = false;
tf_1.setBackground(Color.red);
tf_1.requestFocus();
}
try {
value_2 = Double.parseDouble(tf_2.getText());
} catch (Exception e) {
good = false;
tf_2.setBackground(Color.red);
tf_2.requestFocus();
}
if(good)
tf_3.setText(String.valueOf(value_1 + value_2));
else
tf_3.setText("Bad input");
}
更新
如果你想分解你的代码(通常这是个好主意),你可以这样做
private Double readDoubleOrHighlight(JTextField tf) {
try {
double res = Double.parseDouble(tf.getText());
tf.setBackground(whatever-default-background-is);
return res;
} catch (NumberFormatException e) {
tf.setBackground(Color.red);
tf.requestFocus();
return null;
}
}
private void addOperands() {
Double value_1 = readDoubleOrHighlight(tf_1);
Double value_2 = readDoubleOrHighlight(tf_2);
if((value_1 != null) && (value_2 != null))
tf_3.setText(String.valueOf(value_1 + value_2));
else
tf_3.setText("Bad input");
}
static interface BinaryOp {
double calc(double arg1, double arg2);
}
private void applyBinaryOp(BinaryOp op) {
Double value_1 = readDoubleOrHighlight(tf_1);
Double value_2 = readDoubleOrHighlight(tf_2);
if((value_1 != null) && (value_2 != null)) {
double res = op.calc(value_1, value_2);
tf_3.setText(res);
}
else
tf_3.setText("Bad input");
}
bu_2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
applyBinaryOp(new BinaryOp(){
public double calc(double arg1, double arg2) {
return arg1 * arg2;
}
});
}
});
注意:这里我有效地使用了
Double
作为选项[Double]
,这在Java中是不受支持的(提示:googleOption
,这是一个强大的想法或从开始)
更新#2
这一次引入了高阶函数。
如果您添加这样一段代码
private Double readDoubleOrHighlight(JTextField tf) {
try {
double res = Double.parseDouble(tf.getText());
tf.setBackground(whatever-default-background-is);
return res;
} catch (NumberFormatException e) {
tf.setBackground(Color.red);
tf.requestFocus();
return null;
}
}
private void addOperands() {
Double value_1 = readDoubleOrHighlight(tf_1);
Double value_2 = readDoubleOrHighlight(tf_2);
if((value_1 != null) && (value_2 != null))
tf_3.setText(String.valueOf(value_1 + value_2));
else
tf_3.setText("Bad input");
}
static interface BinaryOp {
double calc(double arg1, double arg2);
}
private void applyBinaryOp(BinaryOp op) {
Double value_1 = readDoubleOrHighlight(tf_1);
Double value_2 = readDoubleOrHighlight(tf_2);
if((value_1 != null) && (value_2 != null)) {
double res = op.calc(value_1, value_2);
tf_3.setText(res);
}
else
tf_3.setText("Bad input");
}
bu_2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
applyBinaryOp(new BinaryOp(){
public double calc(double arg1, double arg2) {
return arg1 * arg2;
}
});
}
});
您可以将单击列表简化为以下内容
private Double readDoubleOrHighlight(JTextField tf) {
try {
double res = Double.parseDouble(tf.getText());
tf.setBackground(whatever-default-background-is);
return res;
} catch (NumberFormatException e) {
tf.setBackground(Color.red);
tf.requestFocus();
return null;
}
}
private void addOperands() {
Double value_1 = readDoubleOrHighlight(tf_1);
Double value_2 = readDoubleOrHighlight(tf_2);
if((value_1 != null) && (value_2 != null))
tf_3.setText(String.valueOf(value_1 + value_2));
else
tf_3.setText("Bad input");
}
static interface BinaryOp {
double calc(double arg1, double arg2);
}
private void applyBinaryOp(BinaryOp op) {
Double value_1 = readDoubleOrHighlight(tf_1);
Double value_2 = readDoubleOrHighlight(tf_2);
if((value_1 != null) && (value_2 != null)) {
double res = op.calc(value_1, value_2);
tf_3.setText(res);
}
else
tf_3.setText("Bad input");
}
bu_2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
applyBinaryOp(new BinaryOp(){
public double calc(double arg1, double arg2) {
return arg1 * arg2;
}
});
}
});
Java 8 lambda语法更简单。
“目前,我很难找到哪个文本字段引发异常……”
——哪个文本字段引发了什么异常?而且异常StActTrink应该告诉你到底是哪一行正在抛出它。而且,你在问为什么你的代码不起作用,为了回答这个问题,我们希望看到的不仅仅是一个片段,而且少于你的整个代码:考虑创建和发布一个完整的问题。请阅读链接以了解有关此有用工具的更多信息。@HovercraftFullOfEels已添加完整内容code@HovercraftFullOfEels我不想使用stackTrace进行调试。我想在输入非双精度值时通知用户。所以我需要区分哪个数值引发了异常“目前我很难正确地找出哪个文本字段引发了异常…”
——哪个文本字段引发了什么异常?而且异常StActTrink应该告诉你到底是哪一行正在抛出它。而且,你在问为什么你的代码不起作用,为了回答这个问题,我们希望看到的不仅仅是一个片段,而且少于你的整个代码:考虑创建和发布一个完整的问题。请阅读链接以了解有关此有用工具的更多信息。@HovercraftFullOfEels已添加完整内容code@HovercraftFullOfEels我不想使用stackTrace进行调试。我想在输入非双精度值时通知用户。所以我需要区分哪个数值抛出了exception@ftp.x32,请查看我的更新。它可能会大大简化您的代码。提示:谷歌选项
??在20亿个结果中,OP应该检查哪一个?@SergGr我现在实施了你的更新#1。但是我觉得更新2对于这个简单的任务来说有点太过分了,不是吗?@ftp.x32这取决于你的任务。更新#2的好处在于它更好地分离了“逻辑”和“UI”。正因为如此,代码重复更少,添加新操作或更改UI(如用其他内容替换JTextField)更容易。另一方面,你应该限制你的完美主义。实际上,我的主要想法是引入这样一个想法,以表明进一步的分离是可能的,即使在这样一个小范围内不需要进一步分离task@SergGr谢谢你的解释。遗憾的是,我未能如更新2所示介绍您的解决方案。或者代码可能不正确。在bu_2s actionListener中调用calc(双arg1,双arg2)
,实际上不起作用。我可以在那里传递文本字段值,但在调用applyBinaryOp
时会再次传递。除非我做错了。@ftp.x32,请查看我的更新。它可能会大大简化您的代码。提示:谷歌选项
??在20亿个结果中,OP应该检查哪一个?@SergGr我现在实施了你的更新#1。但是我觉得更新2对于这个简单的任务来说有点太过分了,不是吗?@ftp.x32这取决于你的任务。更新#2的好处在于它更好地分离了“逻辑”和“UI”。正因为如此,代码重复更少,添加新操作或更改UI(如用其他内容替换JTextField)更容易。另一方面,你应该限制你的完美主义。我的主要想法实际上是整型