J2ME Learning (4) - Separate MIDlet and Interface (Compare Foundation)

xiaoxiao2021-03-06  39

J2ME Learning (4) - Separate MIDlet and Interface (Compare Foundation)

Although J2ME programming is performed using object-oriented ideas, the amount of code is increased (increasing the size of the published file) and the complexity of the code. However, for the maintenanceability and scalability of the code, most programs now export interfaces and logic, and first explain how to separate the MIDlet primary class and interface.

In the interface and MIDlet, the contents of the system you need to switch have two parts: 1, Display object; 2, exit processing in the MIDET.

The sample code is as follows:

Package testmidlet;

Import javax.microedition.midlet. *;

Import javax.microedition.lcdui. *;

Public Class Testmidlet Extends Midlet {

Private static testmidlet installation;

PRIVATE LOGINFORM DISPLAYABLEABLEABLE

Private display display;

/ ** constructor * /

Public TestMidlet () {

Instance = THIS;

Display = display.getdisplay (this);

Displayable = new loginform (display); DISPLAY

}

/ ** main method * /

Public void startapp () {

Display .Setcurrent (Displayable); DISPLAYABLE

}

/ ** Handle Pausing the MIDlet * /

Public void pauseApp () {

}

/ ** Handle Destroying the MIDlet * /

Public void destroyApp (boolean unconditional) {

}

/ ** quit the midlet * /

Public static void quitApp () {

Instance.destroyApp (True);

Instance.notifyDestroyed ();

Instance = NULL;

}

}

Package testmidlet;

Import javax.microedition.lcdui. *;

Public class loginform extends form imports commandlistener {

Private Display Display; / ** constructor * /

Public loginform (Display Display) {

Super ("Test");

THIS.DISPLAY = Display; setcommandlistener (this);

// add the exit command

Addcommand (New Command ("EXIT", Command.exit, 1);

}

/ ** Handle Command Events * /

Public void CommandAction (Command Command, Displayable DisplayAble) {

/ ** @todo add command handling code * /

IF (Command.getCommandType () == command.exit) {

// stop the midlet

Testmidlet.quitApp ();

}

}

}

Where the Display object can be passed through the constructor, the exit method can be executed by method calls. This way, your code can implement the MIDlet class and the interface separation.

转载请注明原文地址:https://www.9cbs.com/read-73216.html

New Post(0)