我正在使用以下代码编写贷款计算器,但是当我尝试验证用户的输入时,我无法让 setText 运行。我没有收到任何编译错误,它只是不起作用。我有以下代码:
public class LoanCalculator extends JFrame {
// Create text fields for interest rate,
// year, loan amount, monthly payment, and total payment
private JTextField jtfAnnualInterestRate = new JTextField("5");
private JTextField jtfNumberOfYears = new JTextField("10");
private JTextField jtfLoanAmount = new JTextField("10000");
private JTextField jtfMonthlyPayment = new JTextField("106.07");
private JTextField jtfTotalPayment = new JTextField("12727.86");
// Create a Compute Payment button
private JButton jbtClear = new JButton("Reset Fields");
public LoanCalculator() {
// Panel p1 to hold labels and text fields
JPanel p1 = new JPanel(new GridLayout(5, 2));
p1.add(new JLabel("Annual Interest Rate"));
p1.add(jtfAnnualInterestRate);
p1.add(new JLabel("Number of Years"));
p1.add(jtfNumberOfYears);
p1.add(new JLabel("Loan Amount"));
p1.add(jtfLoanAmount);
p1.add(new JLabel("Monthly Payment"));
p1.add(jtfMonthlyPayment);
p1.add(new JLabel("Total Payment"));
p1.add(jtfTotalPayment);
p1.setBorder(new
TitledBorder("Enter loan amount, interest rate, and year"));
jtfTotalPayment.setEditable(false);
jtfMonthlyPayment.setEditable(false);
// Panel p2 to hold the button
JPanel p2 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
p2.add(jbtClear);
// Add the panels to the frame
add(p1, BorderLayout.CENTER);
add(p2, BorderLayout.SOUTH);
// Register listeners
jbtClear.addActionListener(new ButtonListener());
jtfAnnualInterestRate.getDocument().addDocumentListener(new DocListener());
jtfNumberOfYears.getDocument().addDocumentListener(new DocListener());
jtfLoanAmount.getDocument().addDocumentListener(new DocListener());
}
/** Handle textfield changes */
class DocListener implements DocumentListener {
public void insertUpdate(DocumentEvent e) { validate(); }
public void removeUpdate(DocumentEvent e) { }
public void changedUpdate(DocumentEvent e) { }
public void validate() {
//get values from text fields
double interest = Double.parseDouble(jtfAnnualInterestRate.getText());
int year = Integer.parseInt(jtfNumberOfYears.getText());
double loanAmt = Double.parseDouble(jtfLoanAmount.getText());
Loan loan = new Loan(interest, year, loanAmt);
//validate field values and shift focus if needed
if (! (interest >= 1 && interest <= 10)){
JOptionPane.showMessageDialog(null,
"interest must be between 1 and 10 percent");
jtfAnnualInterestRate.setText("5"); //THIS DOESN'T RUN
jtfAnnualInterestRate.requestFocus(true);
}
//display monthly and total payments
else {
jtfMonthlyPayment.setText(String.format("%.2f",
loan.getMonthlyPayment()));
jtfTotalPayment.setText(String.format("%.2f",
loan.getTotalPayment()));
}
}
}
/** Handle the Reset button Code not included */
}
public static void main(String[] args) {
LoanCalculator frame = new LoanCalculator();
frame.pack();
frame.setTitle("LoanCalculator");
Dimension screenSize =
Toolkit.getDefaultToolkit().getScreenSize();
int screenWidth = screenSize.width;
int screenHeight = screenSize.height;
// Locate the upper-left corner at (x, y)
int x = 3 * (screenWidth - frame.getWidth()) / 4;
int y = (screenHeight - frame.getHeight()) /2;
frame.setLocation(x, y);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
知道我哪里出错了吗?