-4
我创建了一个贷款计算器。但它没有计算价值。请你帮我找到出了什么问题。为什么java不从GUI中获取数字?
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();
}
}
我有
我有一个计算不同类不同的类,但ATLEAST未服用的价值,甚至在同一个班级。当我输入任何值时,它显示异常错误。 我编辑了代码,缩短了它们。我得到的错误
异常在线程“AWT-EventQueue-0”java.lang.NumberFormatException:对于输入字符串:“” 即使当我把值放入textAreas。
什么是例外? –
这是太多的代码。将其分解为[SSCCE](http://sscce.org),并且不要忘记发布确切的错误消息... – jlordo
线程“AWT-EventQueue-0”中的异常java.lang.NumberFormatException:For输入字符串:“” – Shasi