原文 http://hzxdark.javaeye.com/blog/175628
http://www.3geye.net/?3/viewspace-3177
kbox 是一個MVC2模型的J2ME開源框架,用于快速開發(fā)可擴展的J2ME MIDP應(yīng)用程序。
主頁:http://kbox-mvc.sourceforge.net/
其結(jié)構(gòu)如圖:

所有的可視組件都位于需要經(jīng)由MIDlet來顯示,而MIDlet上的所有事件,包括鍵盤事件,按鈕事件提交到ActionController 上, ActionController跟據(jù)配置文件,將事件轉(zhuǎn)發(fā)給相應(yīng)的Action,Action將處理結(jié)果得到的可視組件(Displayable)通過 ActionForward返回MIDlet顯示。
KBOX的使用非常簡單, 以一個HELLO WORLD 為例:
創(chuàng)建一個MIDLET:
public class TestMIDlet extends MIDlet{
…
protected void startApp() throws MIDletStateChangeException {
ActionController controller = ActionController.getInstance(true);
controller.registerViewer(this); //Register the MIDlet
MyKGameCanvas mgc = new MyKGameCanvas("MYGC"); //Create a GameCanvas
KCommand kc = new KCommand("TEST BUTTON", Command.HELP,1,"MYCMD",null);
mgc.addCommand(kc);
mgc.setCommandListener(controller);
Display.getDisplay(this).setCurrent(mgc);
}
…
}
創(chuàng)建一個Canvas:
public class MyKGameCanvas extends KGameCanvas{
public MyKGameCanvas(String uniqueID) {
super(uniqueID);
}
public void showString(String string){
Graphics g = this.getGraphics();
g.setColor(0xFFFFFFFF);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(0xFF000000);
g.drawString(string, 0, 0, Graphics.TOP|Graphics.LEFT);
this.flushGraphics();
}
}
創(chuàng)建相應(yīng)的Action:
public class TestAction implements Action{
public void excute(KCommand cmd, Item item, Displayable dis, ActionForward forward,Hashtable parameters) {
MyKGameCanvas mkc = ((MyKGameCanvas)dis);
mkc.showString("hello world! From "+mkc.getUniqueID()+"."+cmd.getUniqueID());
}
}
將按鈕事件注冊給Action:
DEBUG=true //open the debug model of KBOX
CMD.MYCMD.ACTION=action.TestAction
注意這里的MyKGameCanvas 繼承的KGameCanvas,而不是GameCanvas。KBOX的KGameCanvas類繼承自GameCanvas,當并沒有使用 GameCanvas那種使用線程監(jiān)聽狀態(tài)的模型,而是重寫了keypressed等方法來處理鍵盤事件。另外,KGameCanvas的重寫的按鈕事件 的處理已經(jīng)實現(xiàn)了重復(fù)按鍵情況,而且可以指定重復(fù)按鍵的靈敏度,這點是非常好用的:)
KCommand是KBOX擴展的按鈕組件。
KCommand kc = new KCommand("TEST BUTTON", Command.HELP,1,"MYCMD",null);
第一個參數(shù)是按鈕的名字,第二參數(shù)是按鈕類型,第三個參數(shù)是優(yōu)先級,第四個參數(shù)是ID名,用于配置文件的映射,對應(yīng)“ CMD.MYCMD.ACTION=action.TestAction”中的MYCMD,第五個參數(shù)是可傳遞參數(shù),是一個Hashtable類型,用于 向Action傳遞參數(shù)(當然你喜歡的話,也可以將參數(shù)提到配置文件里去……)。
http://www.3geye.net/?3/viewspace-3177
kbox 是一個MVC2模型的J2ME開源框架,用于快速開發(fā)可擴展的J2ME MIDP應(yīng)用程序。
主頁:http://kbox-mvc.sourceforge.net/
其結(jié)構(gòu)如圖:

所有的可視組件都位于需要經(jīng)由MIDlet來顯示,而MIDlet上的所有事件,包括鍵盤事件,按鈕事件提交到ActionController 上, ActionController跟據(jù)配置文件,將事件轉(zhuǎn)發(fā)給相應(yīng)的Action,Action將處理結(jié)果得到的可視組件(Displayable)通過 ActionForward返回MIDlet顯示。
KBOX的使用非常簡單, 以一個HELLO WORLD 為例:
創(chuàng)建一個MIDLET:
public class TestMIDlet extends MIDlet{
…
protected void startApp() throws MIDletStateChangeException {
ActionController controller = ActionController.getInstance(true);
controller.registerViewer(this); //Register the MIDlet
MyKGameCanvas mgc = new MyKGameCanvas("MYGC"); //Create a GameCanvas
KCommand kc = new KCommand("TEST BUTTON", Command.HELP,1,"MYCMD",null);
mgc.addCommand(kc);
mgc.setCommandListener(controller);
Display.getDisplay(this).setCurrent(mgc);
}
…
}
創(chuàng)建一個Canvas:
public class MyKGameCanvas extends KGameCanvas{
public MyKGameCanvas(String uniqueID) {
super(uniqueID);
}
public void showString(String string){
Graphics g = this.getGraphics();
g.setColor(0xFFFFFFFF);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(0xFF000000);
g.drawString(string, 0, 0, Graphics.TOP|Graphics.LEFT);
this.flushGraphics();
}
}
創(chuàng)建相應(yīng)的Action:
public class TestAction implements Action{
public void excute(KCommand cmd, Item item, Displayable dis, ActionForward forward,Hashtable parameters) {
MyKGameCanvas mkc = ((MyKGameCanvas)dis);
mkc.showString("hello world! From "+mkc.getUniqueID()+"."+cmd.getUniqueID());
}
}
將按鈕事件注冊給Action:
DEBUG=true //open the debug model of KBOX
CMD.MYCMD.ACTION=action.TestAction
注意這里的MyKGameCanvas 繼承的KGameCanvas,而不是GameCanvas。KBOX的KGameCanvas類繼承自GameCanvas,當并沒有使用 GameCanvas那種使用線程監(jiān)聽狀態(tài)的模型,而是重寫了keypressed等方法來處理鍵盤事件。另外,KGameCanvas的重寫的按鈕事件 的處理已經(jīng)實現(xiàn)了重復(fù)按鍵情況,而且可以指定重復(fù)按鍵的靈敏度,這點是非常好用的:)
KCommand是KBOX擴展的按鈕組件。
KCommand kc = new KCommand("TEST BUTTON", Command.HELP,1,"MYCMD",null);
第一個參數(shù)是按鈕的名字,第二參數(shù)是按鈕類型,第三個參數(shù)是優(yōu)先級,第四個參數(shù)是ID名,用于配置文件的映射,對應(yīng)“ CMD.MYCMD.ACTION=action.TestAction”中的MYCMD,第五個參數(shù)是可傳遞參數(shù),是一個Hashtable類型,用于 向Action傳遞參數(shù)(當然你喜歡的話,也可以將參數(shù)提到配置文件里去……)。