2012-10-08 61 views
2

基本上这个简单的程序所做的是 当你点击按钮时显示摘要 比萨尺寸有三个单选按钮,顶部有三个复选框。 我遇到的问题是用户第一次点击顶部时,然后单击按钮并在MessageDialog中显示适当的摘要,当用户想要没有浇头时,它不会显示“没有浇头选定“复选框ItemListener逻辑比萨订购

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

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

这样,对于每个取消选择,减1和检查计数,如果它是零,只需将该标签设置为“不选顶部”即可见或重新出现。

1

排列流程的方式是不可能在比萨本身之前选择顶部(提交信息时提出条件)。 默认情况下,消息是“不选顶部”,当他选择顶部时,可以在消息的新文本中追加新的顶部。

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