免责声明:这是(令人沮丧的)与家庭作业相关的问题。
当我在屏幕上绘制对象时,我的结果很奇怪。我想要这个......我绘制第一个对象然后绘制第二个对象,当我选择第三个对象来绘制屏幕时,我必须再次启动过程....我得到的是...我绘制第一个对象,我绘制第二个对象,我去绘制第三个对象屏幕清除...我选择第三个对象,但SECOND对象是屏幕上显示的内容。请指点我正确的方向。
package ui.panels;
import java.awt.Choice;
import java.awt.Panel;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import model.Model;
import interfaces.Resettable;
public class ChoicePanel extends Panel implements Resettable{
public int i = 0;
/**
*
*/
private static final long serialVersionUID = 1L;
Model model;
Choice selection;
public ChoicePanel(Model mdl) {
model = mdl;
selection = new Choice();
for (String msg : Model.selections) {
selection.add(msg);
}
selection.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(i==1) {//drop down clicked three times)
System.out.println("ChoicePanel says i == "+i);
model.setMessage(selection.getSelectedItem());
model.setCurrentShapeType(selection.getSelectedItem());
//model.repaint();
++i;
}else if(i==2){
System.out.println("ChoicePanel says i == "+i);
model.setMessage(selection.getSelectedItem());
//model.setCurrentShapeType(selection.getSelectedItem());
model.resetComponents();
//--i;
}else{
model.setMessage(selection.getSelectedItem());
//this line is what sends a value to shape that is drawn on screen
model.setCurrentShapeType(selection.getSelectedItem());
//model.repaint();
++i;
}
}
});
this.add(selection);
}
public void resetComponents() {
System.out.println("resetComponents from ChoicePanel");
//this resets the drop down list selection array to the first choice on the list
selection.select(0);
//this sets selected item in the selection array set in the above line
//model.setMessage(selection.getSelectedItem());
i=0;
model.repaint();
}
}
答案 0 :(得分:3)
在else if(i==2){ ... }
内,您不会增加i
,它永远不会超过2
。
} else if(i==2){
System.out.println("ChoicePanel says i == "+i);
model.setMessage(selection.getSelectedItem());
//model.setCurrentShapeType(selection.getSelectedItem());
model.resetComponents();
++i; // Increment here
}