首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2ME开发 >

多个StringItem平添在form后只显示一个

2012-12-27 
多个StringItem添加在form后只显示一个想把每次选择后的选项显示在StringItem,定义了四个StringItem却只显

多个StringItem添加在form后只显示一个
想把每次选择后的选项显示在StringItem,定义了四个StringItem却只显示一个
[java code]import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class ChoiceGroupMIDlet extends MIDlet implements CommandListener,
ItemStateListener {
private Display display;
private Command exitCommand;
private Form myform;
private ChoiceGroup myExclusiveChoiceGroup;
private ChoiceGroup myMultipleChoiceGroup;
private ChoiceGroup myPopupChoiceGroup;
private StringItem myChoice;
private Image img = null;
private StringItem str01;
private StringItem str02;
private StringItem str03;
private int index;

public ChoiceGroupMIDlet() {
// TODO Auto-generated constructor stub
display = Display.getDisplay(this);
exitCommand = new Command("退出",Command.SCREEN,1);
myform = new Form("ChoiceGroup演示");
try{
img = Image.createImage("/pic.png");
}catch(Exception e){
myform.append("加载图像文件失败");
}
myExclusiveChoiceGroup = new ChoiceGroup("选择性别",ChoiceGroup.EXCLUSIVE);
myMultipleChoiceGroup = new ChoiceGroup("选择学校",ChoiceGroup.MULTIPLE);
myPopupChoiceGroup = new ChoiceGroup("选择婚姻状况",ChoiceGroup.POPUP);
myExclusiveChoiceGroup.append("男", img);
myExclusiveChoiceGroup.append("女", img);
myMultipleChoiceGroup.append("南开大学", img);
myMultipleChoiceGroup.append("天津大学", img);
myMultipleChoiceGroup.append("清华大学", img);
myMultipleChoiceGroup.append("北京大学", img);
myPopupChoiceGroup.append("未婚", img);
myPopupChoiceGroup.append("已婚", img);

myChoice = new StringItem("您选择的内容是:","");
str01 = new StringItem("性别","男");
str02 = new StringItem("学校","");
str03 = new StringItem("婚姻","未婚");

myform.append(myExclusiveChoiceGroup);
myform.append(myMultipleChoiceGroup);
myform.append(myPopupChoiceGroup);

myform.append(myChoice);
//myform.append(str01);
myform.append(str02);
myform.append(str03);
index = myform.append(str01);
System.out.print("插入的位置是:" + index);

myform.addCommand(exitCommand);
myform.setCommandListener(this);
myform.setItemStateListener(this);
}

protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub

}

protected void pauseApp() {
// TODO Auto-generated method stub

}

protected void startApp() throws MIDletStateChangeException {
// TODO Auto-generated method stub
display.setCurrent(myform);
}

public void commandAction(Command c, Displayable d) {
// TODO Auto-generated method stub
if(c == exitCommand){
try {
destroyApp(false);
} catch (MIDletStateChangeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
notifyDestroyed();
}

}

public void itemStateChanged(Item item) {
// TODO Auto-generated method stub
        if(item == myExclusiveChoiceGroup){


        int selected = myExclusiveChoiceGroup.getSelectedIndex();
        String temp = myExclusiveChoiceGroup.getString(selected);
        str01.setText(temp);
        }
        if(item == myMultipleChoiceGroup){//重复出现之前已经选择的选项
        boolean selected;        
        String temp = "";
        for(int i = 0;i < myMultipleChoiceGroup.size();i++){
        selected = myMultipleChoiceGroup.isSelected(i);
        if(selected){
        temp += myMultipleChoiceGroup.getString(i);
        }
        }
        str02.setText(temp);
        }
        if(item == myPopupChoiceGroup){
        int selected = myPopupChoiceGroup.getSelectedIndex();
        String temp = myPopupChoiceGroup.getString(selected);
        str03.setText(temp);
        }
}

}
[\java code]
[解决办法]
你定义了鼠标进行选择的事件吗???
[解决办法]
把4个内容放在1个form中;你确定都添加了?还是你要把字符串连在一起没连上?
[解决办法]
把4个内容放在1个form中;你确定都添加了
[解决办法]
代码没错的,估计是编译过程出错,你新建个项目试试看。

热点排行