簡體   English   中英

JCheckBox在運行時不會取消選擇

[英]JCheckBox does not deselect during runtime

我創建了一個使用單選按鈕,按鈕和復選框的基本程序。 我的問題是,在運行時可以選中復選框,但不能取消選中。 我很確定它很簡單,但是我嘗試了Oracle網站上的代碼片段,但似乎沒有用。 我能指出正確的方向嗎? 謝謝。

這是一個內部類,用於處理復選框的事件。 假設接收事件並將變量設置為適當的價格。 我嘗試了else但這似乎不起作用。

private void buildPanel()
{
    //Create the label, radio buttons, check boxes, and buttons
    messageLabel = new JLabel("Select the package of your choice: ");
    min1 = new JRadioButton("300 Minutes - $45.00 per month.");
    min2 = new JRadioButton("800 Minutes - $65.00 per month.");
    min3 = new JRadioButton("1500 Minutes -  $99.00 per month.");

    model1 = new JRadioButton("Model 100 - $29.95");
    model2 = new JRadioButton("Model 110 - $49.95 ");
    model3 = new JRadioButton("Model 200 - $99.95");

    vMail = new JCheckBox("Voice Mail  - $5.00 per month.");
    vMail.setSelected(false);
    textMes = new JCheckBox("Text Messaging - $10.00 per month.");
    textMes.setSelected(false);

    okButton = new Button("OK");

    //Group the radio buttons
    radiogroupmin = new ButtonGroup();
    radiogroupmin.add(min1);
    radiogroupmin.add(min2);
    radiogroupmin.add(min3);

    radiogroupmodel = new ButtonGroup();
    radiogroupmodel.add(model1);
    radiogroupmodel.add(model2);
    radiogroupmodel.add(model3);

    //Group the check boxes
    checkboxgroup = new ButtonGroup();

    checkboxgroup.add(vMail);
    checkboxgroup.add(textMes);

    //Add action listener to the radio buttons
    min1.addActionListener(new RadioButtonListener());
    min2.addActionListener(new RadioButtonListener());
    min3.addActionListener(new RadioButtonListener());

    model1.addActionListener(new RadioButtonListener());
    model2.addActionListener(new RadioButtonListener());
    model3.addActionListener(new RadioButtonListener());

    //Add action listener to the check boxes
    vMail.addItemListener(new CheckBoxListener());
    textMes.addItemListener(new CheckBoxListener());

    //Add action listener to the button
    okButton.addActionListener(new ButtonListener());

    //create a panel and add the components to it
    panel = new JPanel(new BorderLayout());
    panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));

    panel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
    panel.add(messageLabel);
    panel.add(min1);
    panel.add(min2);
    panel.add(min3);


    panel.add(model1);
    panel.add(model2);
    panel.add(model3);


    panel.add(vMail);
    panel.add(textMes);     

    panel.add(okButton);
}

public static class RadioButtonListener implements ActionListener
{       
        public static double minPrice;
        public static String model;
        public static double modelPrice = 0.0;

    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource() == min1)
        {
            minPrice = 45.00;
        }
        else if(e.getSource() == min2)
        {
            minPrice = 65.00;
        }
        else if(e.getSource() == min3)
        {
            minPrice = 99.00;
        }

        if(e.getSource() == model1)
        {
            model = "Model 100";
            modelPrice = 29.00;
        }
        else if(e.getSource() == model2)
        {
            model = "Model 110";
            modelPrice = 49.99;
        }
        else if(e.getSource() == model3)
        {
            model = "Model 200";
            modelPrice = 99.99;
        }
    }


    public static double getMinPrice()
    {
        return minPrice;
    }

    public static  String getModel()
    {
        return model;
    }

    public static double getModelPrice()
    {
        return modelPrice;
    }
}

public static class CheckBoxListener implements ItemListener
{

    static double voicePrice;
    static double  textPrice;
    public void itemStateChanged(ItemEvent e) 
    {
        if(e.getSource() == vMail)
        {
            if (e.getStateChange() == ItemEvent.SELECTED)
            {
            voicePrice = 5.00;
            }
            else
            {
            voicePrice = 0.0;
            }
        }           

        if(e.getSource() == textMes)
        {
            if(e.getStateChange() == ItemEvent.SELECTED)
            {
                textPrice = 10.00;
            }
            else
            {
                textPrice = 0.0;
            }
        }
    }

    public static double getVoicePrice()
    {
        return voicePrice;
    }

    public static double getTextPrice()
    {
        return textPrice;
    }   

}
private  class ButtonListener implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == okButton)
        {
            DecimalFormat df = new DecimalFormat("$00.##");

            double total;
            final double tax = 0.6;
            final double modelTax = (RadioButtonListener.getModelPrice() * tax) + RadioButtonListener.getModelPrice();
            total = RadioButtonListener.getMinPrice() + 
                    modelTax + CheckBoxListener.getVoicePrice() + CheckBoxListener.getTextPrice(); 
            JOptionPane.showMessageDialog(null, "Your total is: " + df.format(total));

        }
    }
}

也許嘗試更改兩個:

if(vMail.isSelected())

和:

if(textMes.isSelected())

至:

if(e.getStateChange() == ItemEvent.SELECTED)

根據新信息修改了以下答案

好的,有幾個問題:

  1. 刪除所有與checkboxgroup相關的代碼。 您不想使用復選框中的ButtonGroup 這是您的主要問題。 ButtonGroup僅通常用於JRadioButtons
  2. 僅創建一個CheckBoxListener實例,即:

    // Add action listener to the check boxes
    CheckBoxListener checkBoxListener = new CheckBoxListener();
    vMail.addItemListener(checkBoxListener);
    textMes.addItemListener(checkBoxListener);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM