A MIDlet with simple text and a few commands. : Command « J2ME « Java Tutorial






import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextBox;
import javax.microedition.midlet.MIDlet;

public class FewCommandsMIDlet extends MIDlet implements CommandListener {
  private Command exitCommand = new Command("A", Command.SCREEN, 1);

  private Command infoCommand = new Command("B", Command.SCREEN, 2);

  private Command aboutCommand = new Command("C", Command.SCREEN, 2);

  private Display display;

  public FewCommandsMIDlet() {
    display = Display.getDisplay(this);
  }

  public void startApp() {
    TextBox t = new TextBox("sdf", "Welcome to MIDP Programming", 256, 0);

    t.addCommand(infoCommand);
    t.addCommand(exitCommand);

    t.addCommand(aboutCommand);
    t.setCommandListener(this);
    display.setCurrent(t);
  }

  public void pauseApp() {
  }

  public void destroyApp(boolean unconditional) {
  }

  public void commandAction(Command c, Displayable s) {
    if (c == exitCommand) {
      destroyApp(false);
      notifyDestroyed();
    }
  }
}








31.17.Command
31.17.1.Add Command to MIDlet
31.17.2.Start the MIDlet by creating a list of items and associating the exit command with itStart the MIDlet by creating a list of items and associating the exit command with it
31.17.3.A MIDlet with simple text and a few commands.
31.17.4.Get command label and typeGet command label and type
31.17.5.Exit Command
31.17.6.Help commandHelp command