程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> J2ME >> J2ME學習(四)——將MIDlet和界面分離(比較基礎)

J2ME學習(四)——將MIDlet和界面分離(比較基礎)

編輯:J2ME
雖然使用面向對象的思想進行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類和界面分離了.

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved