我正在使用Java的GUI创建(嗯,已经创建)一个贷款支付计算器。有些计算是错误的,我会改正的,所以不用介意。我真正需要帮助的是用户输入验证。我尝试使用try/catch块,但我一直在捕获异常的NetBeans中获得红色下划线。为什么我得到了下划线,这是正确的验证类型吗?让我知道如果更多的代码或信息是需要的。下面是主类/驱动程序代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
public class LoanCalculator extends JFrame {
// Create text fields for interest rate, years
// loan amount, monthly payment, and total payment
private static final JTextField INTEREST_RATE = new JTextField();
private static final JTextField LOAN_YEARS = new JTextField();
private static final JTextField LOAN_AMOUNT = new JTextField();
private static final JTextField MONTHLY_PAYMENT = new JTextField();
private static final JTextField TOTAL_PAYMENT = new JTextField();
// Create a Compute Payment button
private static final JButton LOAN_COMPUTE = new JButton("Compute Payment");
public LoanCalculator() {
// Panel p1 to hold labels and text fields
//setSize (FRAME_WIDTH, FRAME_HEIGHT);
JPanel labelPanel = new JPanel(new GridLayout(6, 2));
labelPanel.add(new JLabel("Annual Interest Rate"));
labelPanel.add(INTEREST_RATE);
labelPanel.add(new JLabel("Number of Years"));
labelPanel.add(LOAN_YEARS);
labelPanel.add(new JLabel("Loan Amount"));
labelPanel.add(LOAN_AMOUNT);
labelPanel.add(new JLabel("Monthly Payment"));
labelPanel.add(MONTHLY_PAYMENT);
labelPanel.add(new JLabel("Total Payment"));
labelPanel.add(TOTAL_PAYMENT);
labelPanel.setBorder(new TitledBorder("Enter loan amount, interest rate, and years"));
// Panel p2 to hold the button
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
buttonPanel.add(LOAN_COMPUTE);
// Add the panels to the frame
add(labelPanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
// Register listener
LOAN_COMPUTE.addActionListener(new ButtonListener());
}
/**
* Handle the Compute Payment button
*/
private class ButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// Get values from text fields
try{
double interest = Double.parseDouble(INTEREST_RATE.getText());
int year = Integer.parseInt(LOAN_YEARS.getText());
double loanAmount = Double.parseDouble(LOAN_AMOUNT.getText());
// Create a loan object
Loan loan = new Loan(interest, year, loanAmount);
// Display monthly payment and total payment
MONTHLY_PAYMENT.setText(String.format("%.2f", loan.getMonthlyPayment()));
TOTAL_PAYMENT.setText(String.format("%.2f", loan.getTotalPayment()));
}catch (Exception ex){
JOptionPane.showMessageDialog(this, ex.getMessage(), "Error," , JOptionPane.ERROR_MESSAGE));
}
}
}
public static void main(String[] args) {
LoanCalculator frame = new LoanCalculator();
frame.pack();
frame.setTitle("LoanCalculator");
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
JOptionPane showMessageDialog方法的第一个参数应该是一个GUI组件,而您的不是,因为它是内部侦听器类。所以修改这个:
JOptionPane.showMessageDialog(this, ex.getMessage(),
"Error,", JOptionPane.ERROR_MESSAGE));
:
JOptionPane.showMessageDialog(LoanCalculator.this, ex.getMessage(),
"Error", JOptionPane.ERROR_MESSAGE); // extra parenthesis removed
顺便说一句,简单地捕捉Exception
通常不是一个好主意,因为那将把你的渔网撒在太大的区域上。更好的方法是捕获特定的和可能预期的异常。
另外,从这个问题中得到的教训是,您将需要仔细阅读所有错误和异常消息,因为它们通常会准确地告诉您哪里出了问题,并且通常并不难理解。