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

J2ME

开发平台:

Java

  1. import javax.microedition.lcdui.*;
  2. import javax.microedition.midlet.MIDlet;
  3. public class ItemCommandListenerDemo extends MIDlet 
  4. {
  5.     private final static Command CMD_STOP = new Command("暂停", 
  6.                                                         Command.ITEM, 1);
  7.     private final static Command CMD_RESTART = new Command("重新开始", 
  8.                                                         Command.ITEM, 2);
  9.     private final static Command CMD_EXIT = new Command("退出", 
  10.                                                         Command.ITEM, 3);
  11.     private Display display;
  12.     private NonInteractiveGaugeRunnable nonInteractive;
  13.     private Form mainForm;
  14.     private Thread mThread;
  15.     
  16.     public ItemCommandListenerDemo() {
  17.         display = Display.getDisplay(this);
  18.         mainForm = new Form("Gauge演示");
  19.         
  20.         
  21.         nonInteractive = new NonInteractiveGaugeRunnable(
  22.                                  "非交互模式", 10, 0);
  23.         mThread = new Thread(nonInteractive);
  24.         mThread.start();
  25.         mainForm.append(nonInteractive);
  26.         nonInteractive.addCommand(CMD_STOP);
  27.         nonInteractive.addCommand(CMD_RESTART);
  28.         nonInteractive.addCommand(CMD_EXIT);
  29.         ItemCommandListener listener = new ItemCommandListener(){
  30.         public void commandAction(Command command, Item item) {
  31.         if(item == nonInteractive){
  32.         if(command == CMD_STOP)nonInteractive.stop();
  33.         if(command == CMD_RESTART)nonInteractive.reStart();
  34.         if(command == CMD_EXIT){
  35.                 destroyApp(false);
  36.                 notifyDestroyed();
  37.         }
  38.         }
  39.         }
  40.         };
  41.         nonInteractive.setItemCommandListener(listener);
  42.     }
  43.     /**
  44.      * Signals the MIDlet to start and enter the Active state.
  45.      */
  46.     protected void startApp() {
  47.         
  48.         display.setCurrent(mainForm);
  49.     }
  50.     /**
  51.      * Signals the MIDlet to terminate and enter the Destroyed state.
  52.      */
  53.     protected void destroyApp(boolean unconditional) {
  54.         nonInteractive.setDone();
  55.     }
  56.     /**
  57.      * Signals the MIDlet to stop and enter the Paused state.
  58.      */
  59.     protected void pauseApp() {
  60.     }
  61. }