`
1140566087
  • 浏览: 547693 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
博客专栏
2c4ae07c-10c2-3bb0-a106-d91fe0a10f37
c/c++ 入门笔记
浏览量:18076
3161ba8d-c410-3ef9-871c-3e48524c5263
Android 学习笔记
浏览量:309479
Group-logo
J2ME 基础学习课程集
浏览量:17993
A98a97d4-eb03-3faf-af96-c7c28f709feb
Spring 学习过程记录...
浏览量:17195
社区版块
存档分类
最新评论

J2ME ChoiceGroup 组件测试

    博客分类:
  • J2ME
阅读更多
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);
		}
	}

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics