日期:2014-05-20  浏览次数:20808 次

选项卡效果的实现
共入手j2me,想利用选项卡做个界面,希望高手指点指点

------解决方案--------------------
Java code
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class ListImage extends MIDlet implements CommandListener {
    private Display display;
    
    //定义List菜单的内容
    private static String menuItems[] = {"Image1",
            "Image2",
            "No Image",
            "Exit"
    };

    private Image image1;
    private Image image2;
    
    private List lstImplicitImage;
    
    public ListImage() {
        try {
            image1 = Image.createImage("/Image1.gif");
            image2 = Image.createImage("/Image2.gif");
        } catch(Exception e) {
            System.out.println("Image can not be loaded: " + e);
        }
        
        lstImplicitImage = new List(
                "List Image Demo",                  //标题
                List.IMPLICIT,                      //隐式类型 
                menuItems,                          //设置列表内容   
                new Image[]{                        //列表用图标
                    image1, image1, image1, image2
                }); 
    }

    protected void startApp() throws MIDletStateChangeException {
        //设置命令监听
        lstImplicitImage.setCommandListener(this);
        
        //获得当前MIDlet的Display对象
        display = Display.getDisplay(this); 
        //设置List对象为当前显示对象
        display.setCurrent(lstImplicitImage);
    }

    protected void pauseApp() {
        // TODO Auto-generated method stub

    }

    protected void destroyApp(boolean arg0) 
        throws MIDletStateChangeException {
        // TODO Auto-generated method stub

    }
    
    /**
     * 处理命令按钮事件
     */
    public void commandAction(Command c, Displayable d) {
        if (c == List.SELECT_COMMAND) {
            //获得被选中选项的索引
            switch(lstImplicitImage.getSelectedIndex()) {
            case 0:
                setImage(image1);
                break;
            case 1:
                setImage(image2);
                break;
            case 2:
                setImage(null);
                break;
            case 3:
                notifyDestroyed();
            }
        }
    }
    
    private void setImage(Image img) {
        for(int i=0;i<lstImplicitImage.size();i++) {
            lstImplicitImage.set(i, 
                    lstImplicitImage.getString(i), img);
        }
    }
}