import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class SavingAccountFrame extends JFrame {
private static final int FRAME_WIDTH = 300;
private static final int FRAME_LENGTH = 500;
private static final double INITIAL_BALANCE = 0.0;
private static final double ANNUAL_RATE = 0.0;
private static final int YEARS = 0;
String[] result;
private JLabel initialLabel;
private JLabel rate;
private JLabel years;
private JTextField initialBal;
private JTextField annualRate;
private JTextField numOfYears;
private JButton calculate;
private JPanel panel;
private JList box;
private JScrollPane scroll;
SavingAccountFrame(){
createTextField();
createButton();
createScrollPane();
createPanel();
setSize(FRAME_WIDTH, FRAME_LENGTH);
}
private void createTextField(){
final int FIELD_WIDTH = 10;
initialLabel = new JLabel("Initial Balance");
initialBal = new JTextField(FIELD_WIDTH);
rate = new JLabel("Annual Rate");
annualRate = new JTextField(FIELD_WIDTH);
years = new JLabel("Number of Years");
numOfYears = new JTextField(FIELD_WIDTH);
}
private void createButton(){
calculate = new JButton("Calculate");
class CalcListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
double iB = Double.parseDouble(initialBal.getText());
double r = Double.parseDouble(rate.getText());
int y = Integer.parseInt(years.getText());
r = r / 100;
for (int i = 0; i < y; i++) {
double newbalance = iB * r;
iB += newbalance;
String test = String.valueOf(iB);
result[i] = test;
}
box = new JList(result);
scroll = new JScrollPane(box);
getContentPane().add(scroll);
}
}
ActionListener d = new CalcListener();
calculate.addActionListener(d);
}
private void createScrollPane(){
scroll = new JScrollPane();
}
private void createPanel()
{
panel = new JPanel();
panel = new JPanel();
panel.add(initialLabel);
panel.add(initialBal);
panel.add(rate);
panel.add(annualRate);
panel.add(years);
panel.add(numOfYears);
panel.add(calculate);
panel.add(scroll);
add(panel);
}
}
import javax.swing.JFrame;
public class SavingAccount {
public static void main(String[] args) {
JFrame frame = new SavingAccountFrame();
frame.setTitle("Savings Account");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
我有一些家庭作业的麻烦,当我按下计算按钮时,我的代码一直在吐出这个错误。
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "Annual Rate"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1242)
at java.lang.Double.parseDouble(Double.java:527)
at SavingAccountFrame$1CalcListener.actionPerformed(SavingAccountFrame.java:54)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2012)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2335)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:404)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6268)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6033)
at java.awt.Container.processEvent(Container.java:2045)
at java.awt.Component.dispatchEventImpl(Component.java:4629)
at java.awt.Container.dispatchEventImpl(Container.java:2103)
at java.awt.Component.dispatchEvent(Component.java:4455)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4633)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4297)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4227)
at java.awt.Container.dispatchEventImpl(Container.java:2089)
at java.awt.Window.dispatchEventImpl(Window.java:2517)
at java.awt.Component.dispatchEvent(Component.java:4455)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:649)
at java.awt.EventQueue.access$000(EventQueue.java:96)
at java.awt.EventQueue$1.run(EventQueue.java:608)
at java.awt.EventQueue$1.run(EventQueue.java:606)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:116)
at java.awt.EventQueue$2.run(EventQueue.java:622)
at java.awt.EventQueue$2.run(EventQueue.java:620)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:619)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)
有人可以告诉我这个例外是什么以及如何解决它。我无法确定哪里似乎指向null或格式不正确。拜托,谢谢
答案 0 :(得分:1)
错误在这里
double r = Double.parseDouble(rate.getText());
在createButton中。 相反,你应该使用annualRate.parseDouble。 因为rate是JLabel而不是textfield。
rate = new JLabel("Annual Rate");
当您尝试将“年费率”解析为数字时,它会为您提供java.lang.NumberFormatException