Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
User interface 黑莓-如何创建子菜单?_User Interface_Blackberry_Java Me_Menu_Submenu - Fatal编程技术网

User interface 黑莓-如何创建子菜单?

User interface 黑莓-如何创建子菜单?,user-interface,blackberry,java-me,menu,submenu,User Interface,Blackberry,Java Me,Menu,Submenu,我想为BB应用程序创建子菜单 当我点击菜单项时,它显示 Option 1 Option 2 Option 3 当我点击选项3时,它会显示出来 1 2 3 作为子菜单项 使用j2me+eclipse子菜单不是标准BlackBerry API的一部分。如果要执行此操作,您必须创建自己的自定义菜单+子菜单控件。始终希望执行此操作) class Scr扩展主屏幕{ 子菜单=新建子菜单(); 公共Scr(){ 对于(int i=0;i

我想为BB应用程序创建子菜单 当我点击菜单项时,它显示

Option 1
Option 2
Option 3
当我点击选项3时,它会显示出来

1
2
3
作为子菜单项


使用j2me+eclipse子菜单不是标准BlackBerry API的一部分。如果要执行此操作,您必须创建自己的自定义菜单+子菜单控件。

始终希望执行此操作)

class Scr扩展主屏幕{
子菜单=新建子菜单();
公共Scr(){
对于(int i=0;i<3;i++){
子菜单SMEMNU=新建子菜单();
添加(新的子菜单项(i+“项”,sMenu));
对于(int k=0;k<3;k++){
子菜单sSMenu=新建子菜单();
添加(新的子菜单项(i+“-”+k+“项”,sSMenu));
对于(int l=0;l<3;l++){
sSMenu
.添加新的子项(i+“-”+k+“-”+l
+"项目);;
}
}
}
添加(新LabelField(“测试菜单”,可聚焦){
受保护的void makeContextMenu(ContextMenu ContextMenu){
menu.mRectangle.x=this.getContentRect().X2();
menu.mRectangle.y=this.getContentRect().Y2();
Ui.getUiEngine().pushScreen(菜单);
EventInjector.InvokeeEvent(新的KeyEvent)(KeyEvent.KEY\u关闭,
字符,0);
}
});
}
}
类子菜单扩展PopupScreen实现ListFieldCallback{
私有静态最终int MAX_WIDTH=Font.getDefault().getAdvance(
“最大菜单项文本”);
XYRect mRectangle=新的XYRect();
向量mSubMenuItems=新向量();
ListField-mListField;
公共子菜单(){
super(新的子菜单项管理器(),默认关闭);
int rowHeight=getFont().getHeight()+2;
mListField=newlistfield(){
受保护的布尔导航单击(int状态,int时间){
runMenuItem(getSelectedIndex());
返回super.navigationClick(状态、时间);
}
};
mListField.setRowHeight(rowHeight);
添加(mListField);
mListField.setCallback(此);
updateNuitems();
}
公共作废添加(子项子项){
mSubMenuItems.addElement(子菜单项);
subnumitem.mmemon=此;
updateNuitems();
}
私有void updateNuitems(){
int rowCounts=mSubMenuItems.size();
mRectangle.width=getMaxObjectToStringWidth(mSubMenuItems);
mRectangle.height=mListField.getRowHeight()*行计数;
mListField.setSize(行数);
}
私有void runMenuItem(int索引){
子菜单项=(子菜单项)获取(mListField,index);
if(null!=item.mSubMenu){
item.mSubMenu.setSubmenupposition(getSubNurect(index));
Ui.getUiEngine().pushScreen(item.mSubMenu);
}否则{
item.run();
}
}
私有XYRect GetSubNurect(整数索引){
子菜单项=(子菜单项)获取(mListField,index);
XYRect结果=item.mSubMenu.mRectangle;
结果.x=mRectangle.x+mRectangle.width;
result.y=mRectangle.y+mListField.getRowHeight()*索引;
int testWidth=Display.getWidth()-(mRectangle.width+mRectangle.x);
if(testWidth结果){
如果(宽度>最大宽度){
结果=最大宽度;
打破
}否则{
结果=宽度;
}
}
}
返回结果;
}
公共无效drawListRow(列表字段,图形g,整数i,整数y,整数w){
//画课文。
String text=get(字段,i).toString();
g、 setColor(Color.WHITE);
g、 drawText(文本,0,y,DrawStyle.省略号,w);
}
公共对象get(ListField ListField,int index){
返回mSubMenuItems.elementAt(索引);
}
public int getPreferredWidth(ListField ListField){
返回mRectangle.width;
}
public int indexOfList(ListField ListField,字符串前缀,int start){
返回0;
}
}
类子菜单项管理器扩展了VerticalFieldManager{
公共子菜单项管理器(){
超级(使用所有高度,使用所有宽度);
}
public int getPreferredWidth(){
返回((子菜单)getScreen()).mRectangle.width;
}
public int getPreferredHeight(){
返回((子菜单)getScreen()).mRectangle.height;
}
受保护的空位子布局(int-maxWidth、int-maxHeight){
maxWidth=getPreferredWidth();
maxHeight=getPreferredHeight();
超级子布局(最大宽度、最大高度);
setExtent(最大宽度、最大高度);
}
}
类子菜单项实现可运行{
字符串mName;
子菜单MME;
子菜单mSubMenu;
公共子菜单项(字符串名称){
此(名称,空);
}
公共子菜单项(字符串名称,子菜单){
mName=名称;
mSubMenu=子菜单;
}
公共字符串toString(){
返回mName;
}
公开募捐{
class Scr extends MainScreen {
    SubMenu menu = new SubMenu();

    public Scr() {

        for (int i = 0; i < 3; i++) {
            SubMenu sMenu = new SubMenu();
            menu.add(new SubMenuItem(i + " item", sMenu));
            for (int k = 0; k < 3; k++) {
                SubMenu sSMenu = new SubMenu();
                sMenu.add(new SubMenuItem(i + "-" + k + " item", sSMenu));
                for (int l = 0; l < 3; l++) {
                    sSMenu
                            .add(new SubMenuItem(i + "-" + k + "-" + l
                                    + " item"));
                }
            }
        }

        add(new LabelField("testing menu", FOCUSABLE) {
            protected void makeContextMenu(ContextMenu contextMenu) {
                menu.mRectangle.x = this.getContentRect().X2();
                menu.mRectangle.y = this.getContentRect().Y2();
                Ui.getUiEngine().pushScreen(menu);

                EventInjector.invokeEvent(new KeyEvent(KeyEvent.KEY_DOWN,
                        Characters.ESCAPE, 0));
            }
        });
    }
}

class SubMenu extends PopupScreen implements ListFieldCallback {
    private static final int MAX_WIDTH = Font.getDefault().getAdvance(
            "max menu item text");
    XYRect mRectangle = new XYRect();
    Vector mSubMenuItems = new Vector();
    ListField mListField;

    public SubMenu() {
        super(new SubMenuItemManager(), DEFAULT_CLOSE);
        int rowHeight = getFont().getHeight() + 2;
        mListField = new ListField() {
            protected boolean navigationClick(int status, int time) {
                runMenuItem(getSelectedIndex());
                return super.navigationClick(status, time);
            }
        };
        mListField.setRowHeight(rowHeight);
        add(mListField);
        mListField.setCallback(this);
        updateMenuItems();
    }

    public void add(SubMenuItem subMenuItem) {
        mSubMenuItems.addElement(subMenuItem);
        subMenuItem.mMenu = this;
        updateMenuItems();
    }

    private void updateMenuItems() {
        int rowCounts = mSubMenuItems.size();
        mRectangle.width = getMaxObjectToStringWidth(mSubMenuItems);
        mRectangle.height = mListField.getRowHeight() * rowCounts;
        mListField.setSize(rowCounts);
    }

    private void runMenuItem(int index) {
        SubMenuItem item = (SubMenuItem) get(mListField, index);
        if (null != item.mSubMenu) {
            item.mSubMenu.setSubMenuPosition(getSubMenuRect(index));
            Ui.getUiEngine().pushScreen(item.mSubMenu);
        } else {
            item.run();
        }
    }

    private XYRect getSubMenuRect(int index) {
        SubMenuItem item = (SubMenuItem) get(mListField, index);
        XYRect result = item.mSubMenu.mRectangle;
        result.x = mRectangle.x + mRectangle.width;
        result.y = mRectangle.y + mListField.getRowHeight() * index;
        int testWidth = Display.getWidth() - (mRectangle.width + mRectangle.x);
        if (testWidth < result.width) {
            result.width = testWidth;
        }

        int testHeight = Display.getHeight()
                - (mRectangle.height + mRectangle.y);
        if (testHeight < result.height)
            result.height = testHeight;

        return result;
    }

    public void setSubMenuPosition(XYRect rect) {
        mRectangle = rect;
    }

    protected void sublayout(int width, int height) {
        super.sublayout(mRectangle.width, mRectangle.height);
        setPosition(mRectangle.x, mRectangle.y);
        setExtent(mRectangle.width, mRectangle.height);
    }

    private int getMaxObjectToStringWidth(Vector objects) {
        int result = 0;
        for (int i = 0; i < objects.size(); i++) {
            int width = getFont().getAdvance(objects.elementAt(i).toString());
            if (width > result) {
                if (width > MAX_WIDTH) {
                    result = MAX_WIDTH;
                    break;
                } else {
                    result = width;
                }
            }
        }
        return result;
    }

    public void drawListRow(ListField field, Graphics g, int i, int y, int w) {
        // Draw the text.
        String text = get(field, i).toString();
        g.setColor(Color.WHITE);
        g.drawText(text, 0, y, DrawStyle.ELLIPSIS, w);
    }

    public Object get(ListField listField, int index) {
        return mSubMenuItems.elementAt(index);
    }

    public int getPreferredWidth(ListField listField) {
        return mRectangle.width;
    }

    public int indexOfList(ListField listField, String prefix, int start) {
        return 0;
    }
}

class SubMenuItemManager extends VerticalFieldManager {
    public SubMenuItemManager() {
        super(USE_ALL_HEIGHT | USE_ALL_WIDTH);
    }

    public int getPreferredWidth() {
        return ((SubMenu) getScreen()).mRectangle.width;
    }

    public int getPreferredHeight() {
        return ((SubMenu) getScreen()).mRectangle.height;
    }

    protected void sublayout(int maxWidth, int maxHeight) {
        maxWidth = getPreferredWidth();
        maxHeight = getPreferredHeight();
        super.sublayout(maxWidth, maxHeight);
        setExtent(maxWidth, maxHeight);
    }

}

class SubMenuItem implements Runnable {
    String mName;
    SubMenu mMenu;
    SubMenu mSubMenu;

    public SubMenuItem(String name) {
        this(name, null);
    }

    public SubMenuItem(String name, SubMenu subMenu) {
        mName = name;
        mSubMenu = subMenu;
    }

    public String toString() {
        return mName;
    }

    public void run() {

    }
}