ListDemo.java
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:5k
源码类别:

J2ME

开发平台:

Java

  1. import javax.microedition.lcdui.*;
  2. import javax.microedition.midlet.MIDlet;
  3. public class ListDemo
  4.     extends MIDlet
  5.     implements CommandListener {
  6.     private final static Command CMD_EXIT =
  7.     new Command("Exit", Command.EXIT, 1);
  8.     private final static Command CMD_BACK =
  9.     new Command("Back", Command.BACK, 1);
  10.     private Display display;
  11.     private List mainList;
  12.     private List exclusiveList;
  13.     private List implicitList;
  14.     private List multipleList;
  15.     
  16.     private boolean firstTime;
  17.     public ListDemo() {
  18.         display = Display.getDisplay(this);
  19.         System.out.println(display.getBestImageHeight(Display.LIST_ELEMENT));
  20.         System.out.println(display.getBestImageWidth(Display.LIST_ELEMENT));
  21.         // these are the strings for the choices.
  22.         String[] stringArray = {
  23.             "Option A", 
  24.             "Option B", 
  25.             "Option C", 
  26.             "Option D"
  27.         };
  28.         // the string elements will have no images
  29.         Image[] imageArray = null;
  30.                     try {
  31.                 // load the duke image to place in the image array
  32.                 Image img = Image.createImage("/option.png");
  33.     
  34.     
  35.                 // these are the images and strings for the choices.
  36.                 imageArray = new Image[] {
  37.                     img, 
  38.                     img, 
  39.                     img,
  40.                     img
  41.                 };
  42.             } catch (java.io.IOException err) {
  43.                 // ignore the image loading failure the application can recover.
  44.             }
  45.         /*
  46.  * create samples of the supported types:
  47.          *      "Exclusive", "Implicit" and  "Multiple"
  48.  */
  49.         exclusiveList = new List("Exclusive", Choice.EXCLUSIVE, stringArray, 
  50.                                  imageArray);
  51.         exclusiveList.addCommand(CMD_BACK);
  52.         exclusiveList.addCommand(CMD_EXIT);
  53.         exclusiveList.setCommandListener(this);
  54.         implicitList = new List("Implicit", Choice.IMPLICIT, stringArray, 
  55.                                 imageArray);
  56.         implicitList.addCommand(CMD_BACK);
  57.         implicitList.addCommand(CMD_EXIT);
  58.         implicitList.setCommandListener(this);
  59.         multipleList = new List("Multiple", Choice.MULTIPLE, stringArray, 
  60.                                 imageArray);
  61.         multipleList.addCommand(CMD_BACK);
  62.         multipleList.addCommand(CMD_EXIT);
  63.         multipleList.setCommandListener(this);
  64.         
  65.         firstTime = true;
  66.     }
  67.     protected void startApp() {
  68.         if (firstTime) {
  69.             // these are the images and strings for the choices.
  70.             Image[] imageArray = null;
  71.     
  72.             try {
  73.                 // load the duke image to place in the image array
  74.                 Image icon = Image.createImage("/Icon.png");
  75.     
  76.     
  77.                 // these are the images and strings for the choices.
  78.                 imageArray = new Image[] {
  79.                     icon, 
  80.                     icon, 
  81.                     icon
  82.                 };
  83.             } catch (java.io.IOException err) {
  84.                 // ignore the image loading failure the application can recover.
  85.             }
  86.     
  87.             String[] stringArray = {
  88.                 "Exclusive", 
  89.                 "Implicit", 
  90.                 "Multiple"
  91.             };
  92.             mainList = new List("Choose type", Choice.IMPLICIT, stringArray, 
  93.                                 imageArray);
  94.             mainList.addCommand(CMD_EXIT);
  95.             mainList.setCommandListener(this);
  96.             display.setCurrent(mainList);
  97.             firstTime = false;
  98.         }
  99.     }
  100.     protected void destroyApp(boolean unconditional) {
  101.     }
  102.     protected void pauseApp() {
  103.     }
  104.     public void commandAction(Command c, Displayable d) {
  105.         if (d.equals(mainList)) {
  106.             // in the main list
  107.             if (c == List.SELECT_COMMAND) {
  108.                 if (d.equals(mainList)) {
  109.                     switch (((List)d).getSelectedIndex()) {
  110.                         case 0:
  111.                             display.setCurrent(exclusiveList);
  112.                             break;
  113.                         case 1:
  114.                             display.setCurrent(implicitList);
  115.                             break;
  116.                         case 2:
  117.                             display.setCurrent(multipleList);
  118.                             break;
  119.                     }
  120.                 }
  121.             }
  122.         } else {
  123.             // in one of the sub-lists
  124.             if (c == CMD_BACK) {
  125.                 display.setCurrent(mainList);
  126.             }
  127.         }
  128.         if (c == CMD_EXIT) {
  129.             destroyApp(false);
  130.             notifyDestroyed();
  131.         }
  132.     }
  133. }