[英]Proper exception handling with multiple Textfields
這是“ +”按鈕調用的方法的代碼
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class Calc extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField tf_1; // contains first operand
private JTextField tf_2; // contains second operand
private JTextField tf_3; // result
private JButton bu_1; // plus operation
private JButton bu_2; // multiplication
/**
* Create the frame.
*/
public Calc() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 387, 143);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(null);
setContentPane(contentPane);
tf_1 = new JTextField();
tf_1.setBounds(20, 35, 60, 20);
contentPane.add(tf_1);
tf_1.setColumns(10);
tf_2 = new JTextField();
tf_2.setBounds(90, 35, 60, 20);
contentPane.add(tf_2);
tf_2.setColumns(10);
tf_3 = new JTextField();
tf_3.setBounds(272, 35, 70, 20);
contentPane.add(tf_3);
tf_3.setColumns(10);
//button 1
bu_1 = new JButton("+");
bu_1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
addOperands();
}
});
bu_1.setBounds(160, 34, 41, 23);
contentPane.add(bu_1);
//button 2
bu_2 = new JButton("*");
bu_2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
multiplyOperands();
}
});;
bu_2.setBounds(211, 34, 41, 23);
contentPane.add(bu_2);
}
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));
}
private void multiplyOperands() {
// TODO implement multiply operation
}
}
目前,我在努力找出哪個textField引發異常。 上面的代碼不起作用,因為double值未初始化。 但是,如果tf_X值不是數字,我不想僅用0計算。
我也試圖讓addOperands()
返回一個double值,但是在出現異常的情況下不可能返回null
。
我想要的是:
tf_3.setText("");
有人可以幫我嗎?
在您的addOperands
代碼中
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,則有兩種選擇:要么通過簡單地將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");
}
注意:在這里,我有效地使用了Java仍然不支持的Double
作為Option[double]
(提示:google Option
,這是一個強大的主意,或者從Wiki中的Option類型開始)。
更新#2
這次引入了高階函數。 如果添加這樣的一段代碼
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語法,它甚至更簡單。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.