从多个复选框中选择值并附加到jtextarea

时间:2017-07-06 19:44:49

标签: java checkbox textarea

我当前的代码只会附加选中的最后一个复选框。我需要从选中的复选框中获取所有值并附加到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");

更新:         private void jBtnRecieptActionPerformed(java.awt.event.ActionEvent evt){
        //声明局部变量         String item ="&#34 ;;         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");

1 个答案:

答案 0 :(得分: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");