如何计算Checkbox和TextField以获得总计

时间:2017-01-14 18:03:56

标签: java

我正在尝试创建一个计算汽车服务的程序,但我真的不知道如何计算复选框和textField以获得要显示的总数。因此,我所坚持的是,如果客户取消选中一个如何减去该总数,那么如何在总计字段中显示总数。

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class JoesAuto extends JFrame implements ItemListener {
    private JCheckBox oilBox; 
    private JCheckBox lubeBox; 
    private JCheckBox radiatorBox; 
    private JCheckBox transBox; 
    private JCheckBox tireBox; 
    private JCheckBox inspectionBox; 
    private JCheckBox mufflerBox; 
    private JLabel partsLabel; 
    private JLabel laborLabel; 
    private JLabel totalLabel; 
    private JTextField partsField; 
    private JTextField laborField; 
    private JTextField totalField; 
    private JButton calcButton; 
    private JButton resetButton; 
    private JPanel topPanel; 
    private JPanel bottomPanel; 
    private JLabel totPrice; 
    private int totalPrice; 

    public final double OIL_CHANGE = 26.00;
    public final double LUBE_JOB=18.00; 
    public final double RADIATOR_FLUSH=30.00; 
    public final double TRANSMISSION_FLUSH=80.00; 
    public final double INSPECTION=15.00; 
    public final double MUFFLER_REP=100.00;
    public final double TIRE_ROTATION=20.00; 

    public JoesAuto () {
        oilBox = new JCheckBox ("Oil Change"); 
        lubeBox = new JCheckBox ("Lube Job");
        radiatorBox = new JCheckBox ("Radiator Flush"); 
        transBox = new JCheckBox ("Transmission Flush"); 
        tireBox = new JCheckBox("Tire Rotation"); 
        inspectionBox = new JCheckBox("Isnpection"); 
        mufflerBox = new JCheckBox("Muffler Replacement"); 

        topPanel = new JPanel(new GridLayout(7,1)); 
        topPanel.add(oilBox); 
        topPanel.add(lubeBox); 
        topPanel.add(radiatorBox); 
        topPanel.add(transBox);
        topPanel.add(tireBox); 
        topPanel.add(inspectionBox); 
        topPanel.add(mufflerBox); 

        bottomPanel = new JPanel (new GridLayout(3,3)); 
        laborLabel = new JLabel ("Labor"); 
        laborField = new JTextField (10); 
        partsLabel = new JLabel ("Parts"); 
        partsField= new JTextField (10); 
        totalLabel = new JLabel ("Total"); 
        totalField = new JTextField (10); 

        bottomPanel.add(laborLabel); 
        bottomPanel.add(laborField);
        bottomPanel.add(partsLabel);
        bottomPanel.add(partsField);
        bottomPanel.add(totalLabel);
        bottomPanel.add(totalField);

        oilBox.addItemListener(this);
        lubeBox.addItemListener(this);
        radiatorBox.addItemListener(this);
        transBox.addItemListener(this);
        tireBox.addItemListener(this);
        inspectionBox.addItemListener(this);
        mufflerBox.addItemListener(this);

        add(topPanel,BorderLayout.NORTH);
        add(bottomPanel,BorderLayout.CENTER); 
        setVisible(true); 
        setSize(500,500); 
        setLocationRelativeTo(null); 

    }
    @Override
    public void itemStateChanged(ItemEvent e) {
        object source = e.getSource (); 
        if(source==oilBox){
            totalPrice+=26; 
        }

        totPrice.setText(totalPrice);   // won't accept it  
    }

    public static void main (String [] args) {
        new JoesAuto (); 
    }
}

2 个答案:

答案 0 :(得分:1)

编写一个方法来遍历所有复选框,以便在每个复选框事件上计算并调用它,而不依赖于调用者对象。

看一下here来迭代组件。

答案 1 :(得分:0)

第一个问题是您没有将JLabel: totPrice添加到JFrame中。这样做:

totPrice = new JLabel("0"); // Set an initial value of 0.
bottomPanel.add(totPrice);

第二,您不能将整数设置为JLabel。你需要一个字符串。这样做:

totPrice.setText(totalPrice+"");