简体   繁体   English

JRadioButtons和JCheckBox

[英]JRadioButtons and JCheckBox

I have a specific question which I am struggling with so far. 到目前为止,我还有一个具体的问题要解决。 I'm working on a specific application where the user can choose insurance options through JCheckBoxes. 我正在开发一个特定的应用程序,用户可以通过JCheckBoxes选择保险选项。

I have used a ButtonGroup to allow the user to select only one of two insurance types—HMO (health maintenance organization) or PPO (preferred provider organization). 我使用ButtonGroup允许用户仅选择两种保险类型之一-HMO(健康维护组织)或PPO(首选提供者组织)。 I used regular (single)JCheckBoxes for dental insurance and vision insurance options; 我使用常规(单个)JCheckBoxes进行牙科保险和视力保险; the user can select one option, both options, or neither option. 用户可以选择一个选项,两个选项或两个都不选择。 As the user selects each option, its name and price is displayed in a text field; 当用户选择每个选项时,其名称和价格将显示在文本字段中。 the HMO costs $200 per month, the PPO costs $600 per month, the dental coverage adds $75 per month, and the vision care adds $20 per month. HMO每月花费200美元,PPO每月花费600美元,牙科保险每月增加75美元,视力保健每月增加20美元。 When a user deselects an item, the text field is made blank. 当用户取消选择项目时,文本字段将变为空白。 Please see the code I wrote below. 请参阅我在下面编写的代码。

public class JInsurance extends JFrame implements ItemListener{
private JTextField t1;
private JRadioButton hmo, ppo;
private JCheckBox dental, visual;
private JLabel lbl;
private ButtonGroup grp;
public double total;

public JInsurance(){
    setBounds(100, 100, 450, 300);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContentPane().setLayout(null);

    grp = new ButtonGroup();


    hmo = new JRadioButton("HMO");
    hmo.setBounds(21, 53, 109, 23);
    hmo.setActionCommand("hmo");
    grp.add(hmo);
    add(hmo);

    ppo = new JRadioButton("PPO");
    ppo.setBounds(21, 101, 109, 23);
    ppo.setActionCommand("ppo");
    grp.add(ppo);
    add(ppo);


    dental = new JCheckBox("Dental Insurance");
    dental.setBounds(178, 53, 135, 23);
    add(dental);

    visual = new JCheckBox("Visual Insurance");
    visual.setBounds(178, 101, 118, 23);
    add(visual);

    lbl = new JLabel("You have chosen");
    lbl.setFont(new Font("Tahoma", Font.BOLD, 12));
    lbl.setBounds(41, 193, 109, 23);
    add(lbl);

    t1 = new JTextField();
    t1.setBounds(152, 181, 179, 45);
    add(t1);
    t1.setColumns(10);

    hmo.addItemListener(this);
    ppo.addItemListener(this);
    dental.addItemListener(this);
    visual.addItemListener(this);
}

public void itemStateChanged(ItemEvent e){


    if (hmo.isSelected())
        if(dental.isSelected())
            total = 200 + 75;
        else if(dental.isSelected() && visual.isSelected())
            total = 200 + 75 + 20;
        else if(visual.isSelected())
            total = 200 + 20;
    t1.setText(String.valueOf(total));


         if(ppo.equals("PPO"))
        if(dental.isSelected())
            total = 600 + 75;
        else if(dental.isSelected() && visual.isSelected())
            total = 600 + 75 + 20;
        else if(visual.isSelected())
            total = 600 + 20;
        t1.setText(String.valueOf(total));


    }

The main issue is that you weren't able to implement the logic properly in the itemStateChanged() method. 主要问题是您无法在itemStateChanged()方法中正确实现逻辑。 The following code should solve your problem. 以下代码可以解决您的问题。

public void itemStateChanged(ItemEvent e){
    if (hmo.isSelected()) {
        total = 200;
        if(dental.isSelected()) {
            total += 75;
        }
        if(visual.isSelected()) {        
            total += 20;
        }
        t1.setText(String.valueOf(total));
    }
    else if(ppo.isSelected()) {
        total = 600;
        if(dental.isSelected()) {
            total += 75;
        }
        if(visual.isSelected()) {
            total += 20;
        }
        t1.setText(String.valueOf(total));
    }
    else {    // In case you later use grp.clearSelection();
        total = 0;
        t1.setText("");
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM