GUI计算java中的按钮编码错误

时间:2012-09-26 16:11:00

标签: java

好的,首先我正在处理的项目有两个包,首先是包含invoiceCalculations.java的murach.business,其次是包含InvoiceForm.java(JFrame)和SwingValidator.java的murach.forms

我需要为Calculate按钮的actionPerformed事件添加一个事件处理程序。将代码添加到此事件处理程序以获取表单中的客户类型和小计,使用InvoiceCalculations类中的calculateDiscountPct方法根据这些值计算折扣百分比,计算折扣金额和总计,并格式化并显示值形式。

表单有5行,例如第一个客户类型,小计,折扣百分比,折扣金额以及总计及以下计算和退出按钮。

这是堆栈跟踪:

run:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "R"
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1222)
    at java.lang.Double.parseDouble(Double.java:510)
    at murach.forms.InvoiceForm.calculateButtonActionPerformed(InvoiceForm.java:183)
    at murach.forms.InvoiceForm.access$300(InvoiceForm.java:15)
    at murach.forms.InvoiceForm$4.actionPerformed(InvoiceForm.java:81)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.java:6382)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3275)
    at java.awt.Component.processEvent(Component.java:6147)
    at java.awt.Container.processEvent(Container.java:2083)
    at java.awt.Component.dispatchEventImpl(Component.java:4744)
    at java.awt.Container.dispatchEventImpl(Container.java:2141)
    at java.awt.Component.dispatchEvent(Component.java:4572)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4619)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4280)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4210)
    at java.awt.Container.dispatchEventImpl(Container.java:2127)
    at java.awt.Window.dispatchEventImpl(Window.java:2489)
    at java.awt.Component.dispatchEvent(Component.java:4572)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:704)
    at java.awt.EventQueue.access$400(EventQueue.java:82)
    at java.awt.EventQueue$2.run(EventQueue.java:663)
    at java.awt.EventQueue$2.run(EventQueue.java:661)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
    at java.awt.EventQueue$3.run(EventQueue.java:677)
    at java.awt.EventQueue$3.run(EventQueue.java:675)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:674)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
BUILD SUCCESSFUL (total time: 25 seconds)

到目前为止,当我点击代码的计算按钮时,我收到错误。这是我目前的代码,您能否告诉我需要对代码进行哪些更改以及为什么?

private void calculateButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                
      // TODO add your handling code here:
   double c = Double.parseDouble(customertypeTextField.getText());
   double s = Double.parseDouble(subtotalTextField.getText());
   pct d = Percent.parsepct(discountpercentTextField.getText());
   double a = Double.parseDouble(discountamountTextField.getText());
   double total = InvoicCalculations.calculateTotal(c, s, d);
   NumberFormat currecny = NumberFormat.getCurrencyInstance();
   totalTextField.setText(currency.format(total));
}                                               

1 个答案:

答案 0 :(得分:1)

其中一个文本字段包含一个字母,导致Double.parseDouble失败,因为它无效。来自documentation

  

抛出:

NullPointerException - if the string is null

NumberFormatException - if the string does not contain a parsable double.

为防止这种情况,您可以:

  • 确保在点击“计算”按钮时,字段仅包含有效数据。您可以使用InputVerifier
  • 来完成此操作
  • 或在解析输入有效之前进行验证,如果不是则显示弹出窗口或错误消息。

其他人已经提到过,重要的是要强调:do not use Double for representing money