我的程序適用於生日和年齡。我嘗試將我的JTextField字符串轉換爲雙打時遇到問題。我使用瞭解析方法,但仍然收到錯誤。請幫忙!如何將JTextField字符串轉換爲雙精度?
public class MyPaymentFrame extends JFrame implements ActionListener {
JTextField txtAge;
JTextField txtDate;
public MyPaymentFrame() {
Container mycnt = getContentPane();
mycnt.setLayout(new FlowLayout());
Color c = new Color(56, 100, 20);
Font F = new Font("Arial", Font.ITALIC, 20);
mycnt.add(new JLabel("Enter your Age"));
txtAge = new JTextField(15);
mycnt.add(txtAmount);
mycnt.add(new JLabel("Enter birthdate"));
txtDate = new JTextField(10);
mycnt.add(txtDate);
}
if (e.getActionCommand().equals("Clear")) {
txtAge.setText("");
txtDate.setText("");
}
if (e.getActionCommand().equals("Calculate")) {
// Converting String to Double
double Amount = Double.parseDouble(txtMonth);
}
}
public static void main(String[] args) {
Theframe myframe = new Theframe();
}
}
你遇到了什麼錯誤? – robotlos
您是否嘗試打印'txtMonth'來排除故障......?它應該是第一步.... – Maljam
代碼不能編譯。 – null