在开发J2ME的时候我还是用到了MVC来分层,模型、视图、控制.我想搞开发的都应该知道这个东东~~
我这里的包主要有5个如下:
client包中主要是程序的主入口
controller包主要为控制器
moble包主要为模型相当如java中的javabean
util包中主要为一些公共函数和操作rms的类
view包中主要是包括你要显示的界面
下面来讲解一下流程:
在client包中就只有一个类,为程序的入口如下:
/**
* 程序的主入口
* @author bolan
*
*/
public class ClientMidlet extends MIDlet {
private Display display;
private static GUIController controller;
public ClientMidlet(){
super();
display = Display.getDisplay(this);
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
controller=null;
}
protected void pauseApp() {
this.notifyPaused();
}
protected void startApp() throws MIDletStateChangeException {
controller=new GUIController(this);
controller.init();
}
public void setCurrent(Displayable disp){
display.setCurrent(disp);
}
public void setCurrent(Alert alert, Displayable disp){
display.setCurrent(alert, disp);
}
public Displayable getCurrent(){
return display.getCurrent();
}
public Display getCurrentDisplay(){
return display;
}
//退出程序
public void exit(boolean flag){
try{
destroyApp(flag);
notifyDestroyed();
}catch(MIDletStateChangeException e){
e.printStackTrace();
}
}
}
controller控制器中的为控制器类如下:
public class GUIController {
private ClientMidlet clientMidlet;
private MainCanvas mainCanvas;
//让字体同一
public static Font font;
public static int fontWordCharWidth=0; //汉字字符的宽度
public static int fontNumberCharWidth=0; //数字的宽度
public static int fontHigh=0; //字体的高度
/在这里可以定义一些vector来保存数据免得到如果用户选择的为同一支股票又要到服务器中取得到数据
public GUIController(ClientMidlet client){
clientMidlet=client;
//font=Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_LARGE);
font=Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_SMALL);
fontWordCharWidth=font.charWidth('明');
fontNumberCharWidth=font.charWidth('0');
fontHigh=font.getHeight();
}
public void init(){
/可以写入一些初始化的东西,如图片...
setCurrent(new MainCanvas(this));
}
一些界面选择的事件
public static class EventID{
public EventID(){}
public static final byte EVENT_CLICK_INDEX = 0; //进入首页
public static final byte EVENT_EXIT = 1; //退出
}
///让用户切换界面的事件如退出程序
public void handleEvent(int eventID, Object[] obj){
switch(eventID){
case EventID.EVENT_CLICK_INDEX:
mainCanvas=new MainCanvas(this);
mainCanvas.initTwo();
setCurrent(mainCanvas);
break;
case EventID.EVENT_EXIT://退出该系统
System.gc();
Thread.yield();
clientMidlet.exit(false);
break;
}
}
public void setCurrent(Displayable disp){
clientMidlet.setCurrent(disp);
}
public void setCurrent(Alert alert, Displayable disp){
clientMidlet.setCurrent(alert, disp);
}
}
moble中主要为一写模型类,主要是为了方便保存数据
util包中为一些公共的函数和一些常量的设置:
常量类:
public class Resources {
public static final int OFFSET_IMG = 101;
public static final int IMG_WDZX = 101;
public static final int IMG_ZXLL = 102;
public static final int IMG_ZDPH = 103;
public static final int IMG_BKJK = 104;
public static final int IMG_ZJLX = 105;
public static final int IMG_JDTJ = 106;
public static final int IMG_SCZS = 107;
public static final int IMG_QQSC = 108;
public static final int IMG_KFJJ = 109;
public static final int IMG_WTJY = 110;
public static final int IMG_TJHY = 111;
public static final int IMG_XTSD = 112;
public static final int IMG_BOTTOM = 113;
public static final int IMG_CATEGORYLEFT = 114;
public static final int IMG_CATEGORYRIGHT = 115;
protected static Hashtable m_images = new Hashtable();
protected static final String[] IMGNAME_MAP = {"wdzx.png","zxll.png","zdph.png","bkjk.png",
"zjlx.png","jdtj.png","sczs.png","qqsc.png",
"kfjj.png","wtjy.png","tjhy.png","xtsd.png",
"bottom.png","categoryleft.jpg","categoryright.jpg"};
/常量的定义如界面中的一些汉字
键盘是的字
public static final String[][] CATEGORY_JIANPAN_NUMBER ={
{"0","1","2","3","4","5","6","7","8","9","←"},
{"A","B","C","D","E","F","G","H","I","000"},
{"J","K","L","M","N","O","P","Q","R","600"},
{"S","T","U","V","W","S","Y","Z","确定"},
};
///得到IMAGE图片/
public static synchronized Image getImage(int id) {
id -= OFFSET_IMG;
Image img = (Image) m_images.get(new Integer(id));
if (img == null) {
try {
img = Image.createImage("/" + IMGNAME_MAP[id]);
m_images.put(new Integer(id), img);
} catch (Exception e) {
System.out.println("-------"+id);
System.out.println("Error getting resource img "+ IMGNAME_MAP[id] + ">>" + e.getMessage());
e.printStackTrace();
}
}
return img;
}
}
view包中主要为界面的显示