您的位置:首页 > 其它

J2ME高级界面实现页面之间的跳转

2010-12-20 11:07 537 查看

package test;




import java.io.IOException;




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;






/** *//**


 * 


 * @author leilu


 * 


 */


public class MainPage extends MIDlet implements CommandListener




...{


    // m_cmdExit


    private Command m_cmdExit = null;




    // m_cmdOK


    private Command m_cmdOK = null;




    // Display


    private Display display = null;




    // list


    private List list = null;




    //


    private Image icon = null;




    public MainPage()




    ...{


        super();




    }




    protected void startApp() throws MIDletStateChangeException




    ...{


        // display


        display = Display.getDisplay(this);


        // m_cmdExit


        m_cmdExit = new Command("Exit", Command.EXIT, 1);


        // m_cmdOK


        m_cmdOK = new Command("OK", Command.OK, 1);


        // 创建图像


        try




        ...{


            icon = Image.createImage("/Icon.png");


        }


        catch (IOException e)




        ...{


            // TODO Auto-generated catch block


            e.printStackTrace();


        }


        // list


        list = new List("主界面", List.EXCLUSIVE);


        list.append("FirstPage", icon);


        list.append("Secondpage", icon);


        // 增加按钮


        list.addCommand(m_cmdExit);


        list.addCommand(m_cmdOK);


        // 监听


        list.setCommandListener(this);


        // 显示


        display.setCurrent(list);




    }




    protected void pauseApp()




    ...{




    }




    protected void destroyApp(boolean arg0) throws MIDletStateChangeException




    ...{


    }




    public void commandAction(Command c, Displayable d)




    ...{


        String cmd = c.getLabel();


        if (cmd == "OK")




        ...{


            switch (((List) d).getSelectedIndex())




            ...{


            case 0:


                FirstPage first = new FirstPage(display, list);


                display.setCurrent(first);


                break;


            case 1:


                SecondPage second = new SecondPage(display, list);


                display.setCurrent(second);


            default:


                break;


            }


        }


        else if (cmd == "Exit")




        ...{


            try




            ...{


                destroyApp(false);


            }


            catch (MIDletStateChangeException e)




            ...{


                e.printStackTrace();


            }


            notifyDestroyed();




        }


    }




}



 


package test;




import java.io.IOException;




import javax.microedition.lcdui.Command;


import javax.microedition.lcdui.CommandListener;


import javax.microedition.lcdui.Display;


import javax.microedition.lcdui.Displayable;


import javax.microedition.lcdui.Form;


import javax.microedition.lcdui.Image;


import javax.microedition.lcdui.List;


import javax.microedition.lcdui.Ticker;




public class FirstPage extends Form implements CommandListener




...{


    // TICKER_TEXT滚动文字


    private static final String TICKER_TEXT = "这是一个滚动条的例子正在滚动,可以一直滚动除非有触发了停止事件";




    // display


    private Display display;


    //


    private Ticker ticker = null;




    // m_cmdBack


    private Command m_cmdBack = null;


    //list


    private List list= null;




    public FirstPage(Display display,Displayable list)




    ...{


        super("FirstPage");


        //


        this.list = (List)list;


        this.display= display;


        m_cmdBack = new Command("Back", Command.BACK, 1);


        ticker = new Ticker(TICKER_TEXT);


        this.setTicker(ticker);


        //增加图片


        Image icon = null;


        try




        ...{


            icon = Image.createImage("/Java.png");


        }


        catch (IOException e)




        ...{


            e.printStackTrace();


        }


        this.append(icon);


        //增加按钮


        this.addCommand(m_cmdBack);


        this.setCommandListener(this);


        //显示FirstPage


        display.setCurrent(this);




    }




    public void commandAction(Command c, Displayable d)




    ...{


        if (m_cmdBack == c)




        ...{


            display.setCurrent(list);


        }




    }




}




package test;




import javax.microedition.lcdui.Alert;


import javax.microedition.lcdui.AlertType;


import javax.microedition.lcdui.Command;


import javax.microedition.lcdui.CommandListener;


import javax.microedition.lcdui.Display;


import javax.microedition.lcdui.Displayable;


import javax.microedition.lcdui.Form;


import javax.microedition.lcdui.List;


import javax.microedition.lcdui.TextField;


public class SecondPage extends Form implements CommandListener




...{


    // display


    private Display display;




    // Back


    private Command m_cmdBack;




    // Alert


    private Command m_cmdAlert;




    // list


    private List list = null;




    public SecondPage(Display display, Displayable list)




    ...{


        super("Second");


        this.display = display;


        this.list = (List) list;


        //


        m_cmdBack = new Command("Back", Command.BACK, 1);


        m_cmdAlert = new Command("Alert", Command.SCREEN, 1);


        this.append(new TextField("E_mail", "lulei@163.com", 15,


                TextField.EMAILADDR));


        // 增加按钮


        this.addCommand(m_cmdBack);


        this.addCommand(m_cmdAlert);


        this.setCommandListener(this);


        // 显示Form屏幕


        display.setCurrent(this);


    }




    public void commandAction(Command c, Displayable d)




    ...{


        if (m_cmdAlert == c)




        ...{


            // 创建要显示的图标




            // 创建一个Alert屏幕对象,要显示的图标为 img


            Alert info = new Alert("This is a Alert Example");


            info.setType(AlertType.INFO);




            // 设置显示时间


            // info.setTimeout(Alert.FOREVER);


            info.setTimeout(3000);


            // 显示Alert屏幕


            display.setCurrent(info);


        }


        if (m_cmdBack == c)




        ...{


            display.setCurrent(list);


        }




    }




}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  j2me list cmd c
相关文章推荐