最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
J2ME学习(四)――将MIDlet和界面分离(比较基础)
时间:2022-07-02 17:44:48 编辑:袖梨 来源:一聚教程网
虽然使用面向对象的思想进行J2ME的编程,会增加代码量(增加发布文件的大小)和提高代码的复杂性。但是为了代码的可维护性和可扩展性,现在绝大多数的程序还是将界面和逻辑分离开来,下面先说明一下如何将MIDlet主类和界面分离。
在界面和MIDlet中,需要交换的系统内容主要有两部分:1、Display对象;2、MIDlet中的退出处理。
示例代码如下:
package testmidlet;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class TestMIDlet extends MIDlet {
private static TestMIDlet instance;
private LoginForm displayable = new LoginForm();
/** Constructor */
public TestMIDlet() {
instance = this;
}
/** Main method */
public void startApp() {
Display.getDisplay(this).setCurrent(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 implements 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();
}
}
}
其中display对象可以通过构造方法进行传递,退出方法可以通过方法调用来执行.这样,你的代码就能实现MIDlet类和界面分离了.
在界面和MIDlet中,需要交换的系统内容主要有两部分:1、Display对象;2、MIDlet中的退出处理。
示例代码如下:
package testmidlet;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class TestMIDlet extends MIDlet {
private static TestMIDlet instance;
private LoginForm displayable = new LoginForm();
/** Constructor */
public TestMIDlet() {
instance = this;
}
/** Main method */
public void startApp() {
Display.getDisplay(this).setCurrent(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 implements 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();
}
}
}
其中display对象可以通过构造方法进行传递,退出方法可以通过方法调用来执行.这样,你的代码就能实现MIDlet类和界面分离了.
相关文章
- QQ经典版下载安装最新版-QQ经典版轻量版app官方正式版下载 12-17
- 豆包网页版官方入口-豆包网页版一键直达官网 12-17
- 抖音网页版官方入口-在线畅玩douyin.com 12-17
- 土豪漫画官方下载入口-安卓/iOS一键安装APP 12-17
- QQ经典版最新版本下载安装-QQ经典版app官方正版下载地址 12-17
- 漫蛙官网入口网页直达-漫蛙官网入口官方认证链接 12-17