-4

我创建了一个贷款计算器。但它没有计算价值。你能帮我找出问题所在吗?

import java.awt.*;
import javax.swing.*;



public void initUIPanel() {
    jf = new JFrame();
    jf.setTitle("Mortgage Calculator");
    jf.setLocation(100, 200);
    jf.setSize(530, 740);
    jf.setVisible(true);
    jf.setResizable(false);


    GridBagLayout gbl = new GridBagLayout();
    GridBagConstraints gbc = new GridBagConstraints();
    JPanel pane = new JPanel(gbl);
    JPanel pane1 = new JPanel(gbl);
    JPanel pane2 = new JPanel(gbl);
    gbc.anchor = GridBagConstraints.WEST;



    gbc.weightx = 1.0;
    gbc.insets = new Insets(30, 3, 3, 3);
    loanAmount = new JTextField(12);
    gbc.gridx = 1;
    gbc.gridy = 0;
    gbc.fill = GridBagConstraints.BOTH;
    gbl.setConstraints(loanAmount, gbc);
    pane.add(loanAmount);



    loanTerm = new JTextField(15);
    gbc.weightx = 1.0;
    gbc.insets = new Insets(20, 3, 3, 3);
    loanAmount = new JTextField(12);
    gbc.gridx = 1;
    gbc.gridy = 1;
    gbc.fill = GridBagConstraints.BOTH;
    gbl.setConstraints(loanAmount, gbc);
    pane.add(loanAmount);




    gbc.insets = new Insets(30, 0, 3, 0);
    interestRate = new JTextField(4);
    gbc.gridx++;
    gbc.gridy = 3;
    gbc.fill = GridBagConstraints.BOTH;
    gbl.setConstraints(interestRate, gbc);
    pane2.add(interestRate);


    b1 = new JButton("Calculate");
    b1.setPreferredSize(new Dimension(5, 25));
    gbc.gridx = 0;
    gbc.gridy = 5;
    gbc.insets = new Insets(20, 30, 0, 30);
    b1.addActionListener(this);
    gbl.setConstraints(b1, gbc);
    pane2.add(b1);




    jf.add(pane, BorderLayout.NORTH);
    jf.add(pane2, BorderLayout.SOUTH);
    jf.setVisible(true);
    jf.pack();


}

public void actionPerformed(ActionEvent e) {


    double amt, rate, pay;
    int time;
    String name;

    if (e.getSource() == b1) {
        amt = Double.parseDouble(loanAmount.getText());
        rate = Double.parseDouble(interestRate.getText());
        time = Integer.parseInt(loanTerm.getText());


        System.out.println(amt);
        System.out.println(rate);
        System.out.println(time);


    } else {
        jf.dispose();
    }
}




public static void main(String[] args) {
    // TODO Auto-generated method stub
    MortgageCalculator mc = new MortgageCalculator();
    mc.initUIPanel();



}

}

我有不同的课程

我有不同的计算类,但即使在同一个类中也至少不取值。当我输入任何值时,它会显示异常错误。我已经编辑了代码,缩短了它们。我得到的错误

线程“AWT-EventQueue-0”java.lang.NumberFormatException 中的异常:对于输入字符串:“”即使我将值放入 textAreas。

4

1 回答 1

2

您永远不会将其添加JTextField loanTerm到,JPanel pane因此它保留其默认的空String值。发生时ActionListener,空String会导致NumberFormatException.

添加JTextField到容器中:

pane.add(loanTerm);
于 2013-06-02T14:35:22.883 回答