listdemo.java

来自「j2me学习 简单例子」· Java 代码 · 共 136 行

JAVA
136
字号
package example.demoseniorui.list;

import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;
/**
 * <p>J2ME List Demo</p>
 * 
 * @author 刘光辉
 * @version 2009.01.23
 */
public class ListDemo extends MIDlet implements CommandListener {
	
	private static final Command CMD_EXIT = new Command("Exit",Command.EXIT,1);
	private static final Command CMD_BACK = new Command("Back",Command.BACK,1);
	private Display display;
	private List mainList;
	private List exclusiveList;//单选式
	private List implicitList;//隐含式
	private List multipleList;//多选式
	private boolean firstTime;
	
	/**
	 * <p>ListDemo的构造函数</p>
	 * <p>在这个方法中实例化了三个列表,分别是单选式exclusiveList、隐含式implicitList、
	 * 多选式列表multipleList</p>
	 */
	public ListDemo() {
		display = Display.getDisplay(this);
		String[] stringArray = {"Option A","Option B","Option C","Option D"};
		Image[] imageArray = null;
		
		//实例化一个单选式列表
		exclusiveList = new List("Exclusive Menu",List.EXCLUSIVE,stringArray,imageArray);
		exclusiveList.addCommand(CMD_BACK);
		exclusiveList.addCommand(CMD_EXIT);
		exclusiveList.setCommandListener(this);
		//实例化一个隐含式列表
		implicitList = new List("Implicit",List.IMPLICIT,stringArray,imageArray);
		implicitList.addCommand(CMD_BACK);
		implicitList.addCommand(CMD_EXIT);
		implicitList.setCommandListener(this);
		//实例化一个多选式列表
		multipleList = new List("Multiple",List.MULTIPLE,stringArray,imageArray);
		multipleList.addCommand(CMD_BACK);
		multipleList.addCommand(CMD_EXIT);
		multipleList.setCommandListener(this);
		
		firstTime = true;
	}

	/**
	 * <p>启动程序,使MIDlet处于active状态</p>
	 * <p>在这个方法中实例化了一个主列表mainList,并将这个列表设置为当前画面</p>
	 * @see javax.microedition.midlet.MIDlet#startApp()
	 */
	protected void startApp() {
		if(firstTime){
			Image[] imageArray = null;
//			try{
//				Image icon = Image.createImage("/");
//				//这里是相对路径
//				imageArray = new Image[]{icon ,icon ,icon};
//			}catch(Exception e){
//				e.printStackTrace();
//			}
			String[] stringArray = {"Exclusive","Implicit","multiple"};
			//实例化隐含式的主列表
			mainList = new List("Choose Type Menu",Choice.IMPLICIT,stringArray,imageArray);
			mainList.addCommand(CMD_EXIT);
			mainList.setCommandListener(this);
			display.setCurrent(mainList);
			
			firstTime = false;
		}
	}

	/**
	 * <p>暂停程序,使MIDlet处于pause状态</p>
	 * @see javax.microedition.midlet.MIDlet#pauseApp()
	 */
	protected void pauseApp() {
	}

	/**
	 * <p>销毁程序,使MIDlet处于destroyed状态</p>
	 * 
	 * @param boolean unconditional 
	 * <ul>true 释放资源保存数据进入destroyed状态,false 抛出异常保持当前状态</ul>
	 * @see javax.microedition.midlet.MIDlet#destroyApp(boolean)
	 */
	protected void destroyApp(boolean unconditional) {
	}

	/**
	 * <p>命令处理函数,实现commandListener接口</p>
	 * @param Command cmd 命令对象
	 * @param Displayable d 被放置到显示屏显示的对象
	 */
	public void commandAction(Command cmd, Displayable d) {
		//判断当前列表是不是主列表
		if(d.equals(mainList)){
			//显示选择的列表
			if(cmd == List.SELECT_COMMAND){
				switch(((List)d).getSelectedIndex()){
				case 0 :
					display.setCurrent(exclusiveList);
					break;
				case 1 :
					display.setCurrent(implicitList);
					break;
				case 2 :
					display.setCurrent(multipleList);
					break;
				}
			}
		}else{
			//返回
			if(cmd == CMD_BACK){
				display.setCurrent(mainList);
			}
		}
		//退出
		if(cmd == CMD_EXIT){
			destroyApp(false);
			notifyDestroyed();
		}
	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?