package com.brj.test;
import java.io.IOException;
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.Image;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
/**
* 重点练习List类
* @author brj
*
*/
public class TestList extends MIDlet implements CommandListener {
private Display display;
private Command exitCommand;
private Command showExclusive;
private Command showImplicit;
private Command showMultiple;
private Command back;
private TextBox mainFace;
private List exclusive;
private List implicit;
private List multiple;
private String[] listTitle;
private String[] listContent;
private Image[] listImage;
public TestList() {
display = Display.getDisplay(this);
//exit Command button
exitCommand = new Command("Exit", Command.EXIT, 0);
//show different type screen groupware List
showExclusive = new Command("sExclusive", Command.SCREEN, 1);
showImplicit = new Command("sImplicit", Command.SCREEN, 1);
showMultiple = new Command("sMultiple", Command.SCREEN, 1);
//back Command button
back = new Command("Back", Command.BACK, 0);
//show this face when enter to program
mainFace = new TextBox("MainFace", "You can choos the type.", 30,
TextField.ANY);
//the title of three style List
listTitle = new String[] { "ExclusiveTitle", "ImplictTitle",
"MultipleTitle" };
// the choice content of the option
listContent = new String[] { "OptionA", "OptionB", "OptionC", "OptionD" };
//the image of the List which initialize them with null object is ok
try {
listImage = new Image[] { Image.createImage("/girl.png"), null,
null, null };
} catch (IOException e) {
System.out.println("path error------");
e.printStackTrace();
}
//new the objects of the List
exclusive = new List(listTitle[0], ChoiceGroup.EXCLUSIVE, listContent,
listImage);
implicit = new List(listTitle[1], ChoiceGroup.IMPLICIT, listContent,
listImage);
multiple = new List(listTitle[2], ChoiceGroup.MULTIPLE, listContent,
listImage);
}
//when the midLet exit execute this method
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
System.out.println("The Program is destroyed.");
}
//pause action
protected void pauseApp() {
System.out.println("The Program is paused.");
}
//start midlet
protected void startApp() throws MIDletStateChangeException {
mainFace.addCommand(exitCommand);
mainFace.addCommand(showExclusive);
mainFace.addCommand(showImplicit);
mainFace.addCommand(showMultiple);
mainFace.setCommandListener(this);
display.setCurrent(mainFace);
}
//register itself to the monitor
public void commandAction(Command c, Displayable d) {
//eixt
if (c == exitCommand) {
try {
this.destroyApp(false);
} catch (MIDletStateChangeException e) {
e.printStackTrace();
}
this.notifyDestroyed();
}
//Exclusive
if (c == showExclusive) {
exclusive.addCommand(back);
exclusive.setCommandListener(this);
display.setCurrent(exclusive);
}
//Implicit
if (c == showImplicit) {
implicit.addCommand(back);
implicit.setCommandListener(this);
display.setCurrent(implicit);
}
//Multiple
if (c == showMultiple) {
multiple.addCommand(back);
multiple.setCommandListener(this);
display.setCurrent(multiple);
}
//back
if (c == back) {
display.setCurrent(mainFace);
}
}
}