我正在开设一个"银行帐户'类型计划,我的目标是提取和存款,显示更新的余额,显示帐户状态(透支与否)。我只是添加/减去并更新了余额,但是我的JTextFields中的数字是绝对值,并且没有显示负余额(余额< 0)。我需要做某种格式化吗?我已经包含了我的按钮监听器代码以及我将双精度转换为字符串等的一些解析。请帮忙!
depositButton.addActionListener(new DepositListener());
withdrawButton.addActionListener(new WithdrawListener());
//create object of BankAccount, assign values to the text fields
BankAccount myAcct = new BankAccount(1524687, 100.00, "Landon Wiggs");
acctNameF.setText(myAcct.getName() );
acctNumF.setText(Integer.toString( myAcct.getAcctNumber() ) );
acctBalanceF.setText( Double.toString(myAcct.getBalance()));
depositWithdrawAmtF.setText("0.00");
private class DepositListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
double sum = Double.valueOf(depositWithdrawAmtF.getText()) + Double.valueOf(acctBalanceF.getText());
double amt = (Double.parseDouble(depositWithdrawAmtF.getText()));
acctBalanceF.setText(Double.toString(sum));
}
}
private class WithdrawListener implements ActionListener {
public void actionPerformed (ActionEvent event) {
double sum = (Double.valueOf(depositWithdrawAmtF.getText() )) - (Double.valueOf(acctBalanceF.getText() ));
double amt = (Double.parseDouble(depositWithdrawAmtF.getText()));
acctBalanceF.setText(Double.toString(sum));
}
}
答案 0 :(得分:2)
当你退出时,你可能应该从账户资金中扣除提款金额,现在你正在做相反的事情。
更改
double sum = (Double.valueOf(depositWithdrawAmtF.getText() )) - (Double.valueOf(acctBalanceF.getText() ));
到
double sum = Double.valueOf(acctBalanceF.getText()) - Double.valueOf(depositWithdrawAmtF.getText());