1

在我的程序中,我正在创建一个菜单,您可以在其中选择不同的菜肴。问题是,如果他们取消选择某些东西,它仍然会被添加到总数中,并且在提交他们的选择后,我必须重新运行程序才能选择新的选择。当他们取消选中它时,我该怎么做,它仍然没有被添加并在提交后刷新菜单?

这是我的侦听器,用于处理选中复选框的情况,另一个用于计算总数。

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

    }
}
4

2 回答 2

0

试试这个java代码:

YourCheckBox.setSelected (boolean isSelected);
于 2013-04-14T17:27:59.470 回答
0

SelectionListener会表明每个JCheckBox. 这会产生一个问题,即每个复选框的单击操作相互依赖并且难以管理。

为避免这种交叉依赖,您可以ActionListeners为每个组件创建单独的组件。Action接口可用于创建多个具体动作,由此可以将要添加的数量传递到其实现的构造函数中。例如

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;
        }
    }
}
于 2013-04-14T17:31:13.770 回答