1
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 的位置或格式不正确的位置。谢谢,麻烦您了

4

1 回答 1

1

错误在这里

double r = Double.parseDouble(rate.getText());

在创建按钮中。相反,您应该使用 yearRate.parseDouble。因为 rate 是 JLabel 但不是文本字段。

rate = new JLabel("Annual Rate");

当您尝试将“年利率”解析为数字时,它会给您 java.lang.NumberFormatException

于 2012-11-15T06:13:43.497 回答