用于比萨订购的Checkbox ItemListener逻辑

时间:2012-10-08 17:09:19

标签: java checkbox applet awt itemlistener

基本上这个简单的程序是做什么的 单击按钮时显示摘要, 披萨尺寸有三个单选按钮,顶部有三个复选框。 我遇到的问题是当用户第一次点击顶部,然后点击按钮并在MessageDialog中显示相应的摘要后,当用户想要没有浇头时,它将不会显示“No Toppings Selected”

import java.applet.Applet;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.*;
import javax.swing.*;

public class PizzaOrdering extends Applet implements ActionListener, ItemListener {

    Button btnOk = new Button("OK");
    CheckboxGroup cbgSize = new CheckboxGroup();
    Checkbox chkSmall = new Checkbox("Small", cbgSize, false);
    Checkbox chkMedium = new Checkbox("Medium", cbgSize, false);
    Checkbox chkLarge = new Checkbox("Large", cbgSize, false);
    Checkbox chkPep = new Checkbox("Pepperoni");
    Checkbox chkMush = new Checkbox("Mushroom");
    Checkbox chkAnch = new Checkbox("Anchiovies");
    String pizza = "";
    String topping1 = "";
    String topping2 = "";
    String topping3 = "";
    String others = "with no toppings";
    Label lbl1 = new Label("Size");
    Label lbl2 = new Label("Toppings");
    Label spacer = new Label("                                                ");
    Label spacer2 = new Label("                                                ");

    @Override
    public void init() {
        resize(250, 150);
        add(lbl1);
        add(spacer);
        add(chkSmall);
        add(chkMedium);
        add(chkLarge);
        add(lbl2);
        add(spacer2);
        add(chkPep);
        add(chkMush);
        add(chkAnch);
        add(btnOk);
        chkAnch.addItemListener(this);
        chkPep.addItemListener(this);
        chkMush.addItemListener(this);
        btnOk.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == btnOk) {
            if (cbgSize.getSelectedCheckbox() == chkSmall) {
                pizza = "Small";
            }
            if (cbgSize.getSelectedCheckbox() == chkMedium) {
                pizza = "Medium";
            }
            if (cbgSize.getSelectedCheckbox() == chkLarge) {
                pizza = "Large";
            }
            JOptionPane.showMessageDialog(btnOk, "You ordered a " + 
                    pizza + " pizza " + others, "Your Order", WIDTH);
        }
    }

    @Override
    public void itemStateChanged(ItemEvent ex) {
        boolean state1 = false;
        boolean state2 = false;
        boolean state3 = false;
        if (ex.getItemSelectable() == chkMush) {
            state1 = chkMush.getState();
            if (state1 == true) {
                topping1 = "Mushroom";
            } else if (state1 == false) {
                topping1 = "";
                if (state2 == false && state3 == false) {
                    others = "with no toppings";
                }
            }
        }

        if (ex.getItemSelectable() == chkPep) {
            state2 = chkPep.getState();
            if (state2 == true) {
                topping2 = "Pepperoni";
            } else if (state2 == false) {
                topping2 = "";
                if (state1 == false && state3 == false) {
                    others = "with no toppings";
                }
            }
        }

        if (ex.getItemSelectable() == chkAnch) {
            state3 = chkAnch.getState();
            if (state3 == true) {
                topping3 = "Anchiovies";

            } else if (state3 == false) {
                topping3 = "";
                if (state1 == false && state2 == false) {
                    others = "with no toppings";
                }
            }
        }
        others = " with the following topping:" + 
                topping1 + " " + topping2 + " " + topping3;
    }
}

2 个答案:

答案 0 :(得分:2)

当用户未选中或取消选中任何复选框时,甚至不会调用itemStateChanged方法。因此,默认情况下可以看到“No topping selected”标签,只要选择了某些顶部,就可以在itemStatechanged方法中隐藏或删除它。您可以在itemStateChanged方法中使用静态int字段'count',并在每个选定的顶部递增它,并在取消选择的每个顶部递减。

通过这种方式,对于每次取消选择,将计数减少一次并检查计数,如果它为零,只需将该标签设置为“无顶部选定”可见或重新出现。

答案 1 :(得分:1)

安排您的流程,使得在披萨本身之前无法选择顶部(在提交信息时提出条件)。 默认情况下,消息为“未选择顶部”,当他选择顶部时,您会在消息的新文本后附加新的顶部/ s。

我建议你阅读装饰设计模式(http://en.wikipedia.org/wiki/Decorator_pattern),这对你的应用非常有帮助。