【IT168 技术】kbox 是一个MVC2模型的J2ME开源框架,用于快速开发可扩展的J2ME MIDP应用程序。
其结构如图:
所有的可视组件都位于需要经由MIDlet来显示,而MIDlet上的所有事件,包括键盘事件,按钮事件提交到ActionController上,ActionController跟据配置文件,将事件转发给相应的Action,Action将处理结果得到的可视组件(Displayable)通过ActionForward返回MIDlet显示。
KBOX的使用非常简单, 以一个HELLO WORLD 为例:
创建一个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);
}
…
}
…
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);
}
…
}
创建一个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();
}
}
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();
}
}