Java- Reading whether a checkbox is checked or not

故事扮演 提交于 2019-12-29 08:56:07

问题


My program compiles and runs, but here is my problem. I have a checkbox set up for each item, but I only need it to total the items that are checked and not all of the items. Instead its totalling all items regardless of whether or not they are checked.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.GridLayout;
import java.text.*;

public class StudentShopping extends JFrame
{
    public static void main(String[] args) {

        JFrame frame = new JFrame();

        frame.setSize(550, 400); //Sets size of the window
        frame.setTitle("Student Shopping");//Adds title to the GUI
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JCheckBox ClothingCheckBox = new JCheckBox();
        JLabel Clothinglbl = new JLabel("Clothing");
        final JTextField ClothingField = new JTextField(3);
        ClothingField.setText("0");

        JCheckBox BooksCheckBox = new JCheckBox(); 
        JLabel Bookslbl = new JLabel("Books");
        final JTextField BooksField = new JTextField(3);
        BooksField.setText("0");

        JCheckBox SuppliesCheckBox = new JCheckBox();
        JLabel Supplieslbl = new JLabel("Supplies");
        final JTextField SuppliesField = new JTextField(3);
        SuppliesField.setText("0");

        JCheckBox MealPlanCheckBox = new JCheckBox();
        JLabel MealPlanlbl = new JLabel("MealPlan");
        final JTextField MealPlanField = new JTextField(3);
        MealPlanField.setText("0");

        JCheckBox InternetAccessCheckBox = new JCheckBox();
        JLabel InternetAccesslbl = new JLabel("InternetAccess");
        final JTextField InternetAccessField = new JTextField(3);
        InternetAccessField.setText("0");

        final JTextField TotalField = new JTextField(10);
        TotalField.setText("0");


        JLabel ButtonLabel = new JLabel("Press Button for Total");      
        JButton button  = new JButton("Calculate Total");


        JPanel panel = new JPanel();

        panel.setLayout(new GridLayout(6,3));
        panel.add(ClothingCheckBox);
        panel.add(Clothinglbl);
        panel.add(ClothingField);
        panel.add(BooksCheckBox);
        panel.add(Bookslbl);
        panel.add(BooksField);
        panel.add(SuppliesCheckBox);    
        panel.add(Supplieslbl);
        panel.add(SuppliesField);
        panel.add(MealPlanCheckBox);
        panel.add(MealPlanlbl);
        panel.add(MealPlanField);
        panel.add(InternetAccessCheckBox);
        panel.add(InternetAccesslbl);
        panel.add(InternetAccessField);
        panel.add(ButtonLabel);
        panel.add(button);
        panel.add(TotalField);
        frame.add(panel);

        class CalculateListener implements ActionListener {


            public void actionPerformed(ActionEvent event) {

            double Clothing = Double.parseDouble(ClothingField.getText());
            double Books = Double.parseDouble(BooksField.getText()) ;
            double Supplies = Double.parseDouble(SuppliesField.getText());
            double MealPlan = Double.parseDouble(MealPlanField.getText());
            double InternetAccess = Double.parseDouble(InternetAccessField.getText());
            double Total = (Clothing+Books+Supplies+MealPlan+InternetAccess)*100

        DecimalFormat df = new DecimalFormat("$#.00");//creates decimal in currency format
        TotalField.setText(df.format(Total)); //
            }
        }

        ActionListener listener = new CalculateListener();
        button.addActionListener(listener);                     

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);


        }
 }

回答1:


Because you are never checking for selected state of checkBoxes. It should be something like this:

double total = 0;
if(ClothingCheckBox.isSelected() && !ClothingField.getText().isEmpty()) {
    total += Double.parseDouble(ClothingField.getText());
}
if(BooksCheckBox.isSelected() && !BooksField.getText().isEmpty()) {
    total += Double.parseDouble(BooksField.getText());
}
if(SuppliesCheckBox.isSelected() && !SuppliesField.getText().isEmpty()){
    total += Double.parseDouble(SuppliesField.getText());
}
if(MealPlanCheckBox.isSelected() && !MealPlanField.getText().isEmpty()){
    total += Double.parseDouble(MealPlanField.getText());
}
if(InternetAccessCheckBox.isSelected() && !InternetAccessField.getText().isEmpty()){            
     total += Double.parseDouble(InternetAccessField.getText());
}
total = total * 100;
DecimalFormat df = new DecimalFormat("$#.00");
TotalField.setText(df.format(total));

Things I would like to suggest you:

  1. Learn java coding convention and use them.
  2. You should also check for blank values in text fields before parsing them to double. Note that this also doesn't ensure that value in text field is parsable to Double.
  3. It seems like you want to have only numeric value in textField. For that don't use JTextField, use JFormattedTextField.


来源:https://stackoverflow.com/questions/9511147/java-reading-whether-a-checkbox-is-checked-or-not

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!