简体   繁体   English

如何根据所选复选框对总金额进行求和

[英]How do I sum the total amount based on selected checkbox

How can I sum the total amount of all the checkbox I chose? 如何汇总我选择的所有复选框的总数?

  public class Frame extends JFrame implements  ItemListener{
    JLabel lbl1=new JLabel("SERVICES");
    JLabel price1=new JLabel("100.00");
    JLabel price2=new JLabel("200.00");
    JLabel price3=new JLabel("300.00"); 


    JCheckBox haircut=new JCheckBox("Hair Cut");
    JCheckBox fullcolor=new JCheckBox("Full Color");
    JCheckBox hairrebond=new JCheckBox("Hair Rebond");


    JPanel first = new JPanel();
    JPanel second= new JPanel();
    JPanel third = new JPanel();
    double price,total;

 public Frame(){

   FlowLayout flow = (new FlowLayout(FlowLayout.LEFT, 30,30));
   add(lbl1);
   first.add(hairrebond);
   first.add(price1);
   second.add(haircut);
   second.add(price2);
   third.add(fullcolor);
   third.add(price3);

   add(first);
   add(second);
   add(third);


   setLayout(flow);
   setVisible(true);
    setSize(600,600);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }

@Override
public void itemStateChanged(ItemEvent e) {

     if(hairrebond.isSelected()==true){

       price=100;
       total += price;

    }

   if(fullcolor.isSelected()==true){

       price=400;
       total += price;

    }if(haircut.isSelected()==true){

       price=500;
       total += price;

    }

   }

   public static void main(String args[]){

    Frame one = new Frame();

}}

Try this code out. 试试这个代码。 Key is to use a local variable inside itemStateChanged method. 关键是在itemStateChanged方法内使用局部变量。

import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Frame extends JFrame implements ItemListener {

    JLabel lbl1 = new JLabel("SERVICES");
    JLabel price1 = new JLabel("100.00");
    JLabel price2 = new JLabel("200.00");
    JLabel price3 = new JLabel("300.00");

    JCheckBox haircut = new JCheckBox("Hair Cut");
    JCheckBox fullcolor = new JCheckBox("Full Color");
    JCheckBox hairrebond = new JCheckBox("Hair Rebond");

    JPanel first = new JPanel();
    JPanel second = new JPanel();
    JPanel third = new JPanel();
    double price, total;

    public Frame() {

        FlowLayout flow = (new FlowLayout(FlowLayout.LEFT, 30, 30));
        add(lbl1);
        first.add(hairrebond);
        first.add(price1);
        second.add(haircut);
        second.add(price2);
        third.add(fullcolor);
        third.add(price3);

        add(first);
        add(second);
        add(third);

        setLayout(flow);
        setVisible(true);
        setSize(600, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        haircut.addItemListener(this);
        fullcolor.addItemListener(this);
        hairrebond.addItemListener(this);
    }

    @Override
    public void itemStateChanged(ItemEvent e) {
        int sum = 0;
        if (hairrebond.isSelected() == true) {
            sum += 100;

        }

        if (fullcolor.isSelected() == true) {
            sum += 300;
        }
        if (haircut.isSelected() == true) {
            sum += 200;
        }

        total = sum;
        System.out.println(total);

    }

    public static void main(String args[]) {

        Frame one = new Frame();

    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Java中为复选框赋予一个值,例如“选择复选框时=加+ 20英镑”? - How do I give a value to my checkbox in java so for example, “when checkbox is selected = add + £20 total”? 如何获取 for 循环中所有项目的总金额? - How do I get the total amount on all items on a for loop? 如何计算消耗的杯子量并将消耗的总水量存储在阵列中? - How do I count the amount of cups consumed and store the total water amount consumed in an array? 如何在java中打印数组中所有数字的总数或总和? - How do I print the total or sum of all the digits in an array in java? 如何计算正确选择的JRadioButton并打印总的“分数”? - How do I count the correctly selected JRadioButton and Print a total “Score”? 如何使用Checkbox获取所有选定的列表项 - How do I get all the selected list Items using Checkbox 我如何查看是否在GridPane中选择了特定的CheckBox? - How do I see if a specific CheckBox is selected within a GridPane? 如何让程序累积用户每次输入的总点数? - How do I get the program to accumulate total amount of points the user inputs each time? 如何计算基于GUI的程序中的总小时数? - How do I calculate total hours in a GUI-based program? 如果选中了 jtable 中相应复选框的复选框,如何获取另一个单元格的值? - How do I get values of another cell if a checkbox is selected of the corresponding checkbox in jtable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM