- Code: Select all
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class EventEx1 extends MIDlet
implements CommandListener {
Display display = null;
List menu = null;
TextBox input = null;
static final Command backCommand =
new Command("Back", Command.BACK, 0);
static final Command mainMenuCommand =
new Command("Main", Command.SCREEN, 1);
static final Command exitCommand =
new Command("Exit", Command.STOP, 2);
String currentMenu = null;
public EventEx1() {
}
public void startApp() throws MIDletStateChangeException {
display = Display.getDisplay(this);
menu = new List("Main Menu", Choice.IMPLICIT);
menu.append("New Game", null);
menu.append("Saved Game", null);
menu.append("High Score", null);
menu.append("Instructions", null);
menu.append("Settings", null);
menu.addCommand(exitCommand);
menu.setCommandListener(this);
mainMenu();
}
public void pauseApp() {
display = null;
menu = null;
input = null;
}
public void destroyApp(boolean unconditional) {
notifyDestroyed();
}
void mainMenu() {
display.setCurrent(menu);
currentMenu = "Main Menu";
}
public void prepare() {
input = new TextBox("Enter some text: ", "", 6, TextField.ANY);
input.addCommand(backCommand);
input.setCommandListener(this);
input.setString("");
display.setCurrent(input);
}
public void testItem1() {
prepare();
currentMenu = "New Game";
}
public void testItem2() {
prepare();
currentMenu = "Saved Game";
}
public void testItem3() {
prepare();
currentMenu = "High Scores";
}
public void testItem4() {
prepare();
currentMenu = "Instructions";
}
public void testItem5() {
prepare();
currentMenu = "Settings";
}
public void commandAction(Command c, Displayable d) {
String label = c.getLabel();
if (label.equals("Exit")) {
destroyApp(true);
} else if (label.equals("Back")) {
if(currentMenu.equals("New Game") ||
currentMenu.equals("Saved Game") ||
currentMenu.equals("High Scores") ||
currentMenu.equals("Instructions") ||
currentMenu.equals("Settings")){
// go back to menu
mainMenu();
}
} else {
List down = (List)display.getCurrent();
switch(down.getSelectedIndex()) {
case 0: testItem1();break;
case 1: testItem2();break;
case 2: testItem3();break;
case 3: testItem4();break;
case 4: testItem5();break;
}
}
}
}
I have changed the command action function alone, check it, The modification is u didnt find correctly in which current menu the back command is there!!!! If still having doubt kindly reply i shall clear you......[:)]