这是我为从文本框中获取文本、将它们解析为双数据类型并打印它们的产品而编写的代码。那么,是否有任何替代方法可以将字符串解析为 double 并处理其异常......?
...........
public void actionPerformed(ActionEvent ae){
try{
if(ae.getSource()==b1 || ae.getSource()==t2){
String s1=t1.getText();
String s2=t2.getText();
double x=Double.parseDouble(s1);
double y=Double.parseDouble(s2);
double z=x*y;
t3.setText(""+z);
}
if(ae.getSource()==b2){
t1.setText(null);
t2.setText(null);
t3.setText(null);
t1.requestFocus(true);
}
}catch(NumberFormatException nfe){
JOptionPane.showMessageDialog(this,"Please Enter Proper Number in the TextFields");
}
}
...............