下载一个黑莓Eclipse插件,然后新建一个工程,之后新建一个类
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.container.HorizontalFieldManager;



public class BBUIApp extends UiApplication
{
public BBUIApp()

{
HorizontalFieldManager top1 = new HorizontalFieldManager();
ButtonField mySubmitButton = new ButtonField("Connect");
ButtonField mySubmitButton1 = new ButtonField("close");

FieldChangeListener listenerSubmit = new FieldChangeListener()
{

public void fieldChanged(Field field, int context)
{
Dialog.alert("ok");
}
};
mySubmitButton.setChangeListener(listenerSubmit);
top1.add(mySubmitButton);
top1.add(mySubmitButton1);
final MyPopup popUp = new MyPopup(top1);

this.pushScreen(popUp);
}

public static void main(String[] args)
{
BBUIApp bbh = new BBUIApp(); // 创建应用程序
bbh.enterEventDispatcher(); // 进入事件分发循环
}


}

这是一个黑莓CLDC基础应用类,需要继承自UIApplication,还要有main方法,因为黑莓运行CLDC时候会自动调用该方法。构造函数里面丢了两个按钮,添加了一个事件监听并添加了一个PopupScreen的子类,就是弹出菜单的Screen,实现如下:
import net.rim.device.api.system.KeyListener;
import net.rim.device.api.ui.Keypad;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.container.PopupScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;

class MyPopup extends PopupScreen
{

MyPopup(Manager manager)
{
super(manager, DEFAULT_MENU | DEFAULT_CLOSE);//设置整个Style,显示出默认的Menu
}

public void makeMenu(Menu menu, int instance)
{

if (instance == Menu.INSTANCE_DEFAULT)
{
menu.add(_myItem);
}

if (instance == Menu.INSTANCE_CONTEXT)
{//接收context Menu信息
menu.add(_myContextItem);
}
super.makeMenu(menu, instance);
}

protected MenuItem _myItem = new MenuItem("My Item", 100, 10)
{

public void run()
{ // do something here } };
}
};

protected MenuItem _myContextItem = new MenuItem("My Context Item", 100, 10)
{

public void run()
{ // do something here } };
}
};

protected boolean keyChar(char c, int status, int time)
{

if(c==Keypad.KEY_ESCAPE)
{
close();
}
return super.keyChar(c, status, time);
}
}

makeMenu继承自父类,重新写了实现,INSTANCE_DEFAULT是默认菜单,也就是黑莓手机上面的菜单键,INSTANCE_CONTEXT是上下文菜单,也就是按了中间圆球弹出来的菜单。如果运行这个示例,按菜单键则弹出来的是带有My Item选项的菜单,如果按了圆球,则会现出来一个对话框显示ok,点过确定后又会弹出来一个带有My Context Item选项的菜单。需要注意的是,不管这个Manager下面有哪些控件,只要传给了PopupScreen,那么在任何控件上面按圆球都会弹出My Context Item这个菜单。
最后要注意,在项目属性配置中,需要设置为CLDC Application
---------------------------------------------------------
专注移动开发
Android, Windows Mobile, iPhone, J2ME, BlackBerry, Symbian
posted on 2009-12-13 12:18
TiGERTiAN 阅读(1246)
评论(0) 编辑 收藏 所属分类:
J2ME 、
BlackBerry