2012-11-15 4 views
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); 
} 

} 

나는 숙제 문제가 있으며 계산 버튼을 누르면이 오류가 계속 발생합니다.JScrollPane을 사용하여 문자열 배열을 표시하려고하지만 계속 오류가 발생합니다.

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을 가리키는 부분이나 형식이 잘못된 부분을 정확하게 지적 할 수 없습니다. 제발 및 오류가 여기

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

createButton에서 당신에게

답변

1

감사합니다. 대신 annualRate.parseDouble을 사용해야합니다. 비율이 JLabel이지만 텍스트 필드가 아니기 때문에

rate = new JLabel("Annual Rate"); 

당신이 번호로 "연간 비율"을 구문 분석 할 때, 그것은 당신에게 줄 것이다 java.lang.NumberFormatException의