【j2me之家技术频道】这两天又研究了一下MIDlet程序的设计方法,发现通过写一个程序治理器可以大大方便MIDlet编程,并在这个基础上扩展了多语言支持。
在MIDlet编程中,很多系统的属性要通过一个MIDlet实例来访问,比如Display.getDisplay,那么在需要切换界面的类里面你必须把MIDlet实例或者Display实例传送给他,尤其在设计弹出和可以返回的界面时。没有更好的办法吗?又比如程序的暂停功能,有没有根方便的方法把他们集成起来?还有错误处理,程序退出等等,利用以往的方法是不是很不爽?
嗯,真是不爽,于是我写了这个程序治理器:App。首先看他的组织方式:
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
static App instance;
Display disp_;
MIDlet app_;
public static void createInstance(MIDlet app)
{
if(instance==null)
instance=new App();
instance.app_=app;
instance.disp_=Display.getDisplay(app);
}
private App()
{
}
每个MIDlet只需要一个治理器,所以只答应一个实例。这样,我们就可以通过静态方法提供一些系统参数的访问方法:
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
public static String getProperty(String name)
{
return instance.app_.getAppProperty(name);
}
public static Display getDisplay()
{
return instance.disp_;
}
他还应该提供返回前一屏的功能,如何实现呢?既然有了display,难道每次还是要App.getDisplay().setCurrent(xxx)?索性提供一个App.setCurrent():
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
public static void setCurrent(Displayable d)
{
if(d!=instance.old_)
{
instance.old_=instance.disp_.getCurrent();
instance.disp_.setCurrent(d);
}
}
对了,我们就利用这个来实现切换到前一屏的功能:
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
public static void restore()
{
if(instance.old_!=null)
{
instance.disp_.setCurrent(instance.old_);
instance.old_=null;
}
}
接下去该轮到程序暂停和恢复的处理以及一些常用命令
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
public static boolean restart()
{
if(instance.paused_)
{
instance.disp_.setCurrent(instance.bef_);
instance.paused_=false;
return true;
}
else
return false;
}
public static void pause()
{
instance.bef_=instance.disp_.getCurrent();
instance.paused_=true;
}
public static void exit()
{
instance.app_.notifyDestroyed();
}
public static void handleError(String msg, Exception ex)
{
System.out.println(msg "::" ex);
}
下面讲述多语言支持的集成。语言选择当然需要一个界面,就是这个:
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
class LanguageMenu extends List implements CommandListener
{
Command cmdOK_;
Object[] langlist_=new Object[]{
"Chinese","zh_cn",null,
"USA","en_us",null
};
public LanguageMenu()
{
super("Select Language",List.IMPLICIT);
for 上一页12 3 下一页