Java-读取复选框是否被选中 [英] Java- Reading whether a checkbox is checked or not

查看:32
本文介绍了Java-读取复选框是否被选中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序编译并运行,但这是我的问题.我为每个项目设置了一个复选框,但我只需要它来汇总检查的项目而不是所有项目.相反,它会汇总所有项目,而不管它们是否经过检查.

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);


        }
 }

推荐答案

因为您从不检查复选框的选中状态.应该是这样的:

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. 了解java 编码约定并使用它们.
  2. 您还应该在将文本字段解析为双倍之前检查文本字段中的空白值.请注意,这也不能确保文本字段中的值可解析为 Double.
  3. 似乎您只想在 textField 中包含数值.对于不使用 JTextField,请使用 JFormattedTextField.立>
  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.

这篇关于Java-读取复选框是否被选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆