J2ME ChoiceGroup 组件测试

import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

//ChoiceGroup 组件测试
public class ChoiceGroup_test extends MIDlet implements CommandListener {
// 定义设置变量
private Display display;
private Form f;
private Image image;
private Image[] imageArray;
private ChoiceGroup choice;
private ChoiceGroup choice2;
private String[] stringArray1 = {"宫爆鸡丁","回锅肉","西红柿"};
private String[] stringArray2 = {"11:30-12:00","12:00-12:30","12:30-13:00"};
private final Command CMD_EXIT = new Command("eixt",Command.EXIT,1);
private final Command CMD_OK = new Command("OK",Command.OK,1);

//构造方法的编写
public ChoiceGroup_test(){
super();
//获取当前的对象
display = Display.getDisplay(this);
}

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

}

protected void pauseApp() {
// TODO Auto-generated method stub
display.setCurrent(null);
f = null;
}

protected void startApp() throws MIDletStateChangeException {
// TODO Auto-generated method stub
//同样ChoiceGroup 这个组件是Form 的子类
f = new Form("Choice 的演示");
f.append("订餐表");
try{
//获取对应的图片 使用的是相对的路径
Image image = Image.createImage("/images/eclipse.png");

//给之前定义好的数组进行赋值
imageArray = new Image[]{image,image,image};

//创建组件
choice = new ChoiceGroup("选出你的午餐菜谱:",ChoiceGroup.MULTIPLE,stringArray1,imageArray);
choice2 = new ChoiceGroup("选择送餐的时间:",ChoiceGroup.EXCLUSIVE,stringArray2,null);

//将对应的组件添加到Form的对象中
f.append(choice);
f.append(choice2);


}catch(Exception ex){
System.out.println("我是监控异常的!无敌一号!");
}
//添加命令
f.addCommand(CMD_EXIT);
f.addCommand(CMD_OK);
f.setCommandListener(this);

//屏幕显示交互
display.setCurrent(f);
}

public void commandAction(Command c, Displayable d) {
// TODO Auto-generated method stub
// 执行相应的命令 传递对应的组件对象

//如果选择了退出 销毁资源
if(c==CMD_EXIT){
try {
destroyApp(false);
} catch (MIDletStateChangeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
notifyDestroyed();
}

//选择了确定 多选模式, 传入的是个集合
if(c==CMD_OK){
String content = "你选择的午餐有:";
for(int i = 0;i<choice.size();i++){
if(choice.isSelected(i)){
content+=stringArray1[i]; //累加选择的
}
}
String contentTime = "送餐时间为"+stringArray2[choice2.getSelectedIndex()];

f.deleteAll();
f.append(content);
f.append(contentTime);
}
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值