itemcommandexample.java

来自「《J2ME Game Programming》随书光盘源代码」· Java 代码 · 共 62 行

JAVA
62
字号

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class ItemCommandExample extends MIDlet implements
        CommandListener, ItemCommandListener
{
   private Form form;
   private SmileyItem smiley;
   private Command changeEmotion;

   public ItemCommandExample()
   {
      form = new Form("Item Commands");

      // create a smiley item
      smiley = new SmileyItem("Smile");

      // create a command to change the type and add it as a command
      // onto the smiley item.
      changeEmotion = new Command("Change", Command.ITEM, 1);
      smiley.addCommand(changeEmotion);

      // set this class to be the listener for the command
      smiley.setItemCommandListener(this);

      form.append(smiley);
      form.addCommand(new Command("Exit", Command.EXIT, 0));
      form.setCommandListener(this);
   }

   public void startApp()
   {
      Display.getDisplay(this).setCurrent(form);
   }

   public void pauseApp()
   {
   }

   public void destroyApp(boolean unconditional)
   {
   }

   // form command listener
   public void commandAction(Command c, Displayable s)
   {
      if (c.getCommandType() == Command.EXIT)
         notifyDestroyed();
   }

   // item command listener
   public void commandAction(Command command, Item item)
   {
      if (command == changeEmotion)
      {
         smiley.change();
      }
   }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?