gpt4 book ai didi

java - 使用 GUI 的输入验证 (java)

转载 作者:搜寻专家 更新时间:2023-11-01 03:36:25 26 4
gpt4 key购买 nike

我正在使用 Java 中的 GUI 创建(嗯,已经创建)一个贷款支付计算器。有些计算是错误的,我会修正它们,所以不要介意这些。我真正需要帮助的是用户输入验证。我尝试使用 try/catch block ,但我在 NetBeans 中不断出现红色下划线,其中 catch 异常所在。为什么我会得到下划线,这是正确的验证类型吗?让我知道是否需要更多代码或信息。这是主要的类/驱动程序代码:

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 通常不是一个好主意,因为那样会在太大的区域撒下你的捕捉网。更好的方法是捕获特定的和可能预期的异常。

此外,从这个问题中可以吸取的教训是,您需要仔细阅读所有错误和异常消息,因为它们通常会准确告诉您哪里出了问题,而且通常并不难理解。

关于java - 使用 GUI 的输入验证 (java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30265435/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com