2017-07-06 52 views
-1

我的当前代码只会追加上一个选中的复选框。我需要从选中的复选框中获取所有值,并将其附加到jtextarea中以便以后打印。也许是一个数组,但不知道如何追加一个数组。谢谢你的帮助!从多个复选框中选择并附加到jtextarea中的值

编辑:我解决了这个!我没有将值传递给append方法,而是使用append方法获得所需的输出。我发布了更新后的代码。

 private void jBtnRecieptActionPerformed(java.awt.event.ActionEvent evt) {            
    //declare local variables 
    String item = null; 
    int quanity = 0; 
    double priceEa = 0; 
    double total = 0; 

    if (jCTiteliestGolfBall.isSelected()) { 
     quanity = Integer.parseInt(jQty1.getText()); 
     item = jCTiteliestGolfBall.getActionCommand(); 
     priceEa = 1.50; 
     total = (priceEa * quanity); 
    } 
    if (jCTiteliestGolfClubs.isSelected()) { 
     quanity = Integer.parseInt(jQty2.getText()); 
     item = jCTiteliestGolfClubs.getActionCommand(); 
     priceEa = 150.00; 
     total = (priceEa * quanity); 
    } 
    if (jCGregNormanGolfShirt.isSelected()) { 
     quanity = Integer.parseInt(jQty3.getText()); 
     item = jCGregNormanGolfShirt.getActionCommand(); 
     priceEa = 15.00; 
     total = (priceEa * quanity); 
    } 
    if (jCGregNormanGolfHat.isSelected()) { 
     quanity = Integer.parseInt(jQty4.getText()); 
     item = jCGregNormanGolfHat.getActionCommand(); 
     priceEa = 10.00; 
     total = (priceEa * quanity); 
    } 
    if (jCGolfGlove.isSelected()) { 
     quanity = Integer.parseInt(jQty5.getText()); 
     item = jCGolfGlove.getActionCommand(); 
     priceEa = 2.00; 
     total = (priceEa * quanity); 
    } 
    if (jCGolfTees.isSelected()) { 
     quanity = Integer.parseInt(jQty6.getText()); 
     item = jCGolfTees.getActionCommand(); 
     priceEa = .50; 
     total = (priceEa * quanity); 
    } 
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"); 
    LocalDateTime now = LocalDateTime.now(); 
    date = (dtf.format(now)); 


    jTextAreaReciept.append("\tFolly Beach Golf:\n" + item + " " + quanity + " @ " 
      + String.format("%.2f", priceEa) + " = " + String.format("%.2f", total) 
      + "\nSubtotal: " + jTextFieldCostofItems.getText() 
      + "\nTax: " + jTextFieldTax.getText() 
      + "\nTotal: " + jTextFieldTotal.getText() 
      + "\n\nDate/Time: " + date + "\n\n\tThank you for your business"); 

更新: 私人无效jBtnRecieptActionPerformed(java.awt.event.ActionEvent中EVT){
//声明局部变量 字符串项= “”; int quanity = 0; double priceEa = 0; double total = 0;

jTextAreaReciept.append("\tFolly Beach Golf:\n"); 

    if (jCTiteliestGolfBall.isSelected()) { 
     quanity = Integer.parseInt(jQty1.getText()); 
     item = jCTiteliestGolfBall.getActionCommand(); 
     priceEa = 1.50; 
     total = (priceEa * quanity); 
     jTextAreaReciept.append("\n" + item + " " + quanity + " @ " 
      + String.format("%.2f", priceEa) + " = " + String.format("%.2f", total)); 
    } 
    if (jCTiteliestGolfClubs.isSelected()) { 
     quanity = Integer.parseInt(jQty2.getText()); 
     item = jCTiteliestGolfClubs.getActionCommand(); 
     priceEa = 150.00; 
     total = (priceEa * quanity); 
     jTextAreaReciept.append("\n" + item + " " + quanity + " @ " 
      + String.format("%.2f", priceEa) + " = " + String.format("%.2f", total)); 
    } 
    if (jCGregNormanGolfShirt.isSelected()) { 
     quanity = Integer.parseInt(jQty3.getText()); 
     item = jCGregNormanGolfShirt.getActionCommand(); 
     priceEa = 15.00; 
     total = (priceEa * quanity); 
     jTextAreaReciept.append("\n" + item + " " + quanity + " @ " 
      + String.format("%.2f", priceEa) + " = " + String.format("%.2f", total)); 
    } 
    if (jCGregNormanGolfHat.isSelected()) { 
     quanity = Integer.parseInt(jQty4.getText()); 
     item = jCGregNormanGolfHat.getActionCommand(); 
     priceEa = 10.00; 
     total = (priceEa * quanity); 
     jTextAreaReciept.append("\n" + item + " " + quanity + " @ " 
      + String.format("%.2f", priceEa) + " = " + String.format("%.2f", total)); 
    } 
    if (jCGolfGlove.isSelected()) { 
     quanity = Integer.parseInt(jQty5.getText()); 
     item = jCGolfGlove.getActionCommand(); 
     priceEa = 2.00; 
     total = (priceEa * quanity); 
     jTextAreaReciept.append("\n" + item + " " + quanity + " @ " 
      + String.format("%.2f", priceEa) + " = " + String.format("%.2f", total)); 
    } 
    if (jCGolfTees.isSelected()) { 
     quanity = Integer.parseInt(jQty6.getText()); 
     item = jCGolfTees.getActionCommand(); 
     priceEa = .50; 
     total = (priceEa * quanity); 
     jTextAreaReciept.append("\n" + item + " " + quanity + " @ " 
      + String.format("%.2f", priceEa) + " = " + String.format("%.2f", total)); 
    } 
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"); 
    LocalDateTime now = LocalDateTime.now(); 
    date = (dtf.format(now)); 


    jTextAreaReciept.append("\n\nSubtotal: " + jTextFieldCostofItems.getText() 
      + "\nTax: " + jTextFieldTax.getText() 
      + "\nTotal: " + jTextFieldTotal.getText() 
      + "\n\nDate/Time: " + date + "\n\n\tThank you for your business"); 

回答

0

试试这个代码:

private void jBtnRecieptActionPerformed(java.awt.event.ActionEvent evt) {            
    //declare local variables 
    String item = ""; 
    int quanity = 0; 
    double priceEa = 0; 
    double total = 0; 

    if (jCTiteliestGolfBall.isSelected()) { 
     quanity += Integer.parseInt(jQty1.getText()); 
     item += jCTiteliestGolfBall.getActionCommand(); 
     priceEa += 1.50; 
     total += (priceEa * quanity); 
    } 
    if (jCTiteliestGolfClubs.isSelected()) { 
     quanity += Integer.parseInt(jQty2.getText()); 
     item += jCTiteliestGolfClubs.getActionCommand(); 
     priceEa += 150.00; 
     total += (priceEa * quanity); 
    } 
    if (jCGregNormanGolfShirt.isSelected()) { 
     quanity += Integer.parseInt(jQty3.getText()); 
     item += jCGregNormanGolfShirt.getActionCommand(); 
     priceEa += 15.00; 
     total += (priceEa * quanity); 
    } 
    if (jCGregNormanGolfHat.isSelected()) { 
     quanity += Integer.parseInt(jQty4.getText()); 
     item += jCGregNormanGolfHat.getActionCommand(); 
     priceEa += 10.00; 
     total += (priceEa * quanity); 
    } 
    if (jCGolfGlove.isSelected()) { 
     quanity += Integer.parseInt(jQty5.getText()); 
     item += jCGolfGlove.getActionCommand(); 
     priceEa += 2.00; 
     total += (priceEa * quanity); 
    } 
    if (jCGolfTees.isSelected()) { 
     quanity += Integer.parseInt(jQty6.getText()); 
     item += jCGolfTees.getActionCommand(); 
     priceEa += .50; 
     total += (priceEa * quanity); 
    } 
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"); 
    LocalDateTime now = LocalDateTime.now(); 
    date = (dtf.format(now)); 


    jTextAreaReciept.append("\tFolly Beach Golf:\n" + item + " " + quanity + " @ " 
      + String.format("%.2f", priceEa) + " = " + String.format("%.2f", total) 
      + "\nSubtotal: " + jTextFieldCostofItems.getText() 
      + "\nTax: " + jTextFieldTax.getText() 
      + "\nTotal: " + jTextFieldTotal.getText() 
      + "\n\nDate/Time: " + date + "\n\n\tThank you for your business"); 
相关问题