Java 提交后如何取消选中所有复选框?

Java 提交后如何取消选中所有复选框?,java,checkbox,Java,Checkbox,在我的程序中,我正在创建一个菜单,您可以在其中选择不同的菜肴。问题是,如果他们取消选择某个内容,它仍然会被添加到总数中,在提交他们的选择后,我必须重新运行程序以选择新的选择。当他们取消选中它时,我如何使它在提交后仍然不会被添加并刷新菜单 这是我的监听器,用于处理何时选中复选框,另一个用于计算总数 private class SelectionListener implements ActionListener { @Override public void actionPer

在我的程序中,我正在创建一个菜单,您可以在其中选择不同的菜肴。问题是,如果他们取消选择某个内容,它仍然会被添加到总数中,在提交他们的选择后,我必须重新运行程序以选择新的选择。当他们取消选中它时,我如何使它在提交后仍然不会被添加并刷新菜单

这是我的监听器,用于处理何时选中复选框,另一个用于计算总数

 private class SelectionListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent event) {
        if (edamame.isSelected()) {
            total1 = total1 + 4.99;
        } else if (calamari.isSelected()) {
            total1 = total1 + 8.99;
        } else if (shrimpTempura.isSelected()) {
            total1 = total1 + 8.99;
        } else if (tempuraCombo.isSelected()) {
            total1 = total1 + 9.99;
        } else if (gyoza.isSelected()) {
            total1 = total1 + 3.99;
        } else if (pepsi.isSelected()) {
            total2 = total2 + 2.99;
        } else if (sprite.isSelected()) {
            total2 = total2 + 2.99;
        } else if (mtDew.isSelected()) {
            total2 = total2 + 2.99;
        } else if (drPepper.isSelected()) {
            total2 = total2 + 2.99;
        } else if (budlight.isSelected()) {
            total2 = total2 + 4.99;
        } else if (budweiser.isSelected()) {
            total2 = total2 + 4.99;
        } else if (playboy.isSelected()) {
            total3 = total3 + 12.99;
        } else if (california.isSelected()) {
            total3 = total3 + 3.99;
        } else if (volcano.isSelected()) {
            total3 = total3 + 13.99;
        } else if (goldenCali.isSelected()) {
            total3 = total3 + 7.99;
        } else if (shrimpTemp.isSelected()) {
            total3 = total3 + 6.99;
        } else if (tunaR.isSelected()) {
            total3 = total3 + 5.99;
        } else if (avocadoR.isSelected()) {
            total3 = total3 + 4.99;
        } else if (spicyCaliR.isSelected()) {
            total3 = total3 + 5.99;
        } else if (salmonR.isSelected()) {
            total3 = total3 + 5.99;
        } else if (crab.isSelected()) {
            total4 = total4 + 3.99;
        } else if (salmon.isSelected()) {
            total4 = total4 + 3.99;
        } else if (tuna.isSelected()) {
            total4 = total4 + 3.99;
        } else if (whiteT.isSelected()) {
            total4 = total4 + 3.99;
        } else if (eel.isSelected()) {
            total4 = total4 + 3.99;
        } else if (crabstick.isSelected()) {
            total4 = total4 + 3.99;
        } else if (lobster.isSelected()) {
            total4 = total4 + 3.99;
        } else if (bulgogi.isSelected()) {
            total5 = total5 + 18.99;
        } else if (kimchiJi.isSelected()) {
            total5 = total5 + 12.99;
        } else if (kalbee.isSelected()) {
            total5 = total5 + 24.99;
        } else if (soonTofu.isSelected()) {
            total5 = total5 + 11.99;
        } else if (bimbimap.isSelected()) {
            total5 = total5 + 10.99;
        } else if (dolSot.isSelected()) {
            total5 = total5 + 12.99;
        } else if (dukMan.isSelected()) {
            total5 = total5 + 13.99;
        } else if (nakJi.isSelected()) {
            total5 = total5 + 16.99;
        } else if (tempuraU.isSelected()) {
            total5 = total5 + 12.99;
        } else if (nabeyaki.isSelected()) {
            total5 = total5 + 13.99;
        } else if (kalbeeTang.isSelected()) {
            total5 = total5 + 14.99;
        }
    }
}


 private class TotalListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent event) {
        NumberFormat desiredlook = NumberFormat.getCurrencyInstance();
        desiredlook.setMaximumFractionDigits(2);
        desiredlook.setMinimumFractionDigits(2);
        name = getName.getText();
        total = (total1 + total2 + total3 + total4 + total5) * 1.065;
        labelTotal.setText(name + " your total is "
                + desiredlook.format(total));

    }
}
请尝试以下java代码:

YourCheckBox.setSelected (boolean isSelected);

您的
SelectionListener
将指示每个
JCheckBox
都使用一个侦听器。这就产生了一个问题,即每个复选框的单击操作相互依赖,并且很难管理

为了避免这种交叉依赖,您可以为每个组件创建单独的
ActionListeners
。 该接口可用于创建多个具体操作,从而将要添加的数据传递到其实现的构造函数中。比如说

class SelectionListener extends AbstractAction { 

    private final double amount;
    public SelectionListener(double amount) {
        this.amount = amount;
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        JCheckBox checkBox = (JCheckBox)event.getSource();
        if (checkBox.isSelected()) {
            total1 += amount;
        }
    }
}