储蓄计算器GUI

时间:2013-11-04 15:00:26

标签: java user-interface calculator

我的创建按钮方法有问题。编译/运行正常,但在尝试计算时,我收到以下错误:“LoanFrame $ 1CalcListener.actionPerformed(LoanFrame.java:60)”这是我的annualR双重代码。为什么这会给我错误,以及解决方法?

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.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;


public class LoanFrame extends JFrame {



private static final int FRAME_WIDTH = 650;
private static final int FRAME_LENGTH = 300;
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;


 LoanFrame(){
    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 initialB = Double.parseDouble(initialBal.getText());
            double annualR = Double.parseDouble(rate.getText());
            int y = Integer.parseInt(years.getText());
            annualR = annualR / 100;

            for (int i = 0; i < y; i++) {
                double newbalance = initialB * annualR;
                initialB += newbalance;
                String test = String.valueOf(initialB);
                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);
   }
}

编辑:感谢回复,我的错误(修复双解析后(annualRate.getText)

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "Number of Years"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at LoanFrame$1CalcListener.actionPerformed(LoanFrame.java:61)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

3 个答案:

答案 0 :(得分:4)

您正在阅读JLabel而不是JTextField

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

应该是

double annualR = Double.parseDouble(annualRate.getText());

答案 1 :(得分:1)

你在这一行得到NullPointerException

for (int i = 0; i < y; i++) {           
    double newbalance = initialB * annualR;
    initialB += newbalance;
    String test = String.valueOf(initialB);
    result[i] = test; // <-- NPE here
}

因为result从未初始化。

答案 2 :(得分:0)

  

我收到以下错误:   “LoanFrame $ 1CalcListener.actionPerformed(LoanFrame.java:60)”这是   我的年度R双重代码

rate.getText()的文字格式无效。