当前位置: 代码迷 >> J2ME >> 界面跳转解决方案
  详细解决方案

界面跳转解决方案

热度:3175   发布时间:2013-02-25 21:37:54.0
界面跳转
要实现的功能很简单,但总是有问题,麻烦大家看看
首先,主界面:
武汉特色介绍

景点
小吃/小吃街
购物街
。。。

完后点击景点,进入子界面:
黄鹤楼
中山公园
江滩

到此我都实现了,下一步,我点击黄鹤楼,就显示一段描述黄鹤楼的文字,这个位置总报错,程序如下(其中DivPage.java单独运行是可以的,所以错误应该在Main.java):

Main.java

Java code
package wuhan; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class Main extends MIDlet implements CommandListener{ private Display display; private Form form; private Command exit; private Command process; private ChoiceGroup characters; private ChoiceGroup cg; private int characterIndex; private TestCanvas canvas;public Main() {     display = Display.getDisplay(this);     characters = new ChoiceGroup("武汉特色介绍",Choice.EXCLUSIVE);     characters.append("景点", null);     characters.append("小吃/小吃街", null);     characters.append("购物街", null);     characters.append("大型购物商场", null);     characters.append("大型购物超市", null);     characters.append("大学院校", null);     exit = new Command("Exit",Command.EXIT,1);     process = new Command("Process",Command.SCREEN,2);     form = new Form("");     characterIndex = form.append(characters);     form.addCommand(exit);     form.addCommand(process);     form.setCommandListener(this); }     public void startApp(){         display.setCurrent(form);     }         public void pauseApp(){     }     public void destroyApp(boolean arg0){     }         public void commandAction(Command command,Displayable displayable){         if(command==exit){             destroyApp(true);             notifyDestroyed();         }         else if(command==process){            int x=0;            boolean picks[] = new boolean[characters.size()];             characters.getSelectedFlags(picks);             for(;x <picks.length;x++){                 if(picks[x]){                     cg = new ChoiceGroup("",Choice.EXCLUSIVE);                     switch(x){                         case 0: cg.append("黄鹤楼", null);                                 cg.append("中山公园", null);                                 cg.append("江滩", null);                                 break;                         case 1: cg.append("热干面", null);                                 cg.append("豆皮", null);                                 break;                         case 2:                         case 3:                         case 4:                         case 5: break;                         default: break;                     }                    break;                }           }           form.delete(characterIndex);           characterIndex = form.append(cg);           boolean picks2[] = new boolean[cg.size()];          cg.getSelectedFlags(picks2);          if(x==0){              for(int y=0;y<picks2.length;y++){                  if(picks2[y]){                      switch(y){                          case 0: canvas = new TestCanvas(new DivPage());                                  display.setCurrent(canvas);                                  break;                          default: break;                      }                  }              }          }          form.delete(characterIndex);         }    } } 


DivPage.java

Java code
package wuhan;import javax.microedition.lcdui.Canvas;import javax.microedition.lcdui.Command;import javax.microedition.lcdui.CommandListener;import javax.microedition.lcdui.Display;import javax.microedition.lcdui.Displayable;import javax.microedition.lcdui.Font;import javax.microedition.lcdui.Graphics;import javax.microedition.midlet.MIDlet;import javax.microedition.midlet.MIDletStateChangeException;public class DivPage extends MIDlet {    private Display display;    private TestCanvas canvas;    public DivPage() {        display = Display.getDisplay(this);        canvas = new TestCanvas(this);    }    protected void startApp() throws MIDletStateChangeException {        display.setCurrent(canvas);    }    protected void pauseApp() {    }    protected void destroyApp(boolean arg0) {    }    public void exitMIDlet() {        destroyApp(true);        notifyDestroyed();    }}    class TestCanvas extends Canvas implements CommandListener {        private String text = "                            武汉特色介绍"                + "\n一、 景点"                + "\n1、 黄鹤楼"                + "\n        冲决巴山群峰,接纳潇湘云水,浩荡长江在三楚腹地与其最长支流汉水交汇,造就了武汉隔两江而三镇互峙的伟姿。 这里地处江汉平原东缘,鄂东南丘陵余脉起伏于平野湖沼之间,龟蛇两山相夹,江上舟辑如织黄鹤楼天造地设于斯。"                + "\n        黄鹤楼是古典与现代熔铸、诗化与美意构筑的精品。她处在山川灵气动荡吐钠的交点,正好映和中华民族喜好登高的民风民俗、亲近自然的空间意识、崇尚宇宙的哲学观念。登黄鹤楼,不仅仅获得愉快,更能使心灵与宇宙意象互渗互融,从而使心灵净化。这大约就是黄鹤楼美的魅力经风雨而不衰,与日月共长存原因之所在。"                + "\n        巍峨耸立于武昌蛇山的黄鹤楼,享有“天下绝景”的盛誉,与湖南岳阳楼,江西滕王阁并称为“江南三大名楼”。唐代诗人崔颢一首“昔人已乘黄鹤去,此地空余黄鹤楼。黄鹤一去不复返,白云千载空悠悠。晴川历历汉阳树,芳草萋萋鹦鹉洲。日暮乡关何处是,烟波江上使人愁。”已成为千古绝唱,更使黄鹤楼名声大噪。全楼各层布置有大型壁画、楹联、文物等。楼外铸铜黄鹤造型、胜像宝塔、牌坊、轩廊、亭阁等一批辅助建筑,将主楼烘托得更加壮丽。登楼远眺,“极目楚天舒”,不尽长江滚滚来,三镇风光尽收眼底。";        private int sreenWidth;        private int sreenHeight;        private int scrollHeight;        private int scrollY;        private int regionY;        private int numHeight;        private Command exit;        private DivPage divPage;        public TestCanvas(DivPage divPage) {            this.divPage = divPage;            exit = new Command("Exit", Command.EXIT, 1);            addCommand(exit);            setCommandListener(this);            try {                this.sreenHeight = this.getHeight();                this.sreenWidth = this.getWidth();            } catch (Exception e) {                e.printStackTrace();            }        }        protected void paint(Graphics g) {            g.setColor(0xFFFFFF);            g.fillRect(0, 0, this.sreenWidth, this.sreenHeight);            Font font = g.getFont();            g.setColor(0x00000);            int row = drawWrapString(g, text, 0, regionY, sreenWidth - 18, font);            if (this.numHeight == 0)                this.numHeight = (row + 1) * font.getHeight();            System.out.println("numHeigth=" + numHeight);            this.scrollHeight = this.sreenHeight * this.sreenHeight / numHeight;            this.scrollY = (-this.regionY) * this.sreenHeight / numHeight;            g.setColor(255);            g.fillRect(this.sreenWidth - 6, 0, 6, this.sreenHeight);            g.setColor(0xFFFFFF);            g.fillRect(this.sreenWidth - 5, this.scrollY, 4, this.scrollHeight);        }        public void keyPressed(int keyCode) {            System.out.println(keyCode);            System.out.println(sreenHeight);            if (keyCode == -2) {                if (this.sreenHeight - this.regionY >= this.numHeight)                    return;                this.regionY -= 30;            } else if (keyCode == -1) {                if (this.regionY >= 0)                    return;                this.regionY += 30;            }            this.repaint();        }        public void commandAction(Command command, Displayable displayable) {            if (command == exit)                divPage.exitMIDlet();        }            public static int drawWrapString(Graphics g, String str, int x, int y,            int rightMargin, Font font) {        int row = 0;        int leftMargin = x;        int fW = 0;        int fH = font.getHeight();        char c;        for (int i = 0; i < str.length(); i++) {            c = str.charAt(i);            if (c == '\r')                continue;            fW = font.charWidth(c);            if (c == '\n' || (x + fW) > rightMargin) {                x = leftMargin;                 y += fH;                 row++;            }            if (c != '\n') {                g.drawChar(c, x, y, 20);                x += fW;             }        }        return row;    }}
  相关解决方案