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

將MIDlet和界面分離

編輯:關於JAVA

雖然使用面向對象的思想進行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 ;
private Display display;
/** Constructor */
public TestMIDlet() {
instance = this;
display = Display.getDisplay(this);
displayable= new LoginForm(display);
}
/** Main method */
public void startApp() {
display .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