1

好的,首先我正在处理的项目有两个包,第一个是包含 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));
}                                               
4

1 回答 1

1

其中一个文本字段包含一个字母,Double.parseDouble由于它无效而导致失败。从文档

抛出:

NullPointerException - if the string is null

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

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

  • 确保当您点击计算按钮时,字段仅包含有效数据。您可以通过使用InputVerifier来做到这一点。
  • 或在解析输入有效之前验证输入是否有效,如果无效则显示弹出窗口或错误消息。

其他人也提到过,这里要强调的很重要:不要Double用于代表金钱

于 2012-09-26T18:29:41.550 回答