⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 textfieldtest.java

📁 大量j2me源代码
💻 JAVA
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
 * A demonstration of a TextField.
 * @author Martin J. Wells
 */
public class TextFieldTest extends MIDlet implements CommandListener,
        ItemStateListener
{
   private Form form;
   private TextField textFieldItem;
   private Command quit;

   /**
    * MIDlet constructor that creates a form and then adds in a TextField item
    * and a quit command.
    */
   public TextFieldTest()
   {
      // Construct a form.
      form = new Form("Text Field Test");

      // Construct the textfield item and a quit command
      textFieldItem = new TextField("Enter text:", "", 10, TextField.ANY);
      quit = new Command("Quit", Command.EXIT, 2);

      // Add everything to the form.
      form.addCommand(quit);
      form.append(textFieldItem);

      // And register us as the listening for both commands and item state
      // changes.
      form.setCommandListener(this);
      form.setItemStateListener(this);
   }

   /**
    * Called by the Application Manager when the MIDlet is starting or resuming
    * after being paused. In this example it acquires the current Display object
    * and uses it to set the Form object created in the MIDlet constructor as
    * the active Screen to display.
    * @throws MIDletStateChangeException
    */
   protected void startApp() throws MIDletStateChangeException
   {
      Display.getDisplay(this).setCurrent(form);
   }

   /**
    * Called by the MID's Application Manager to pause the MIDlet. A good
    * example of this is when the user receives an incoming phone call whilst
    * playing your game. When they're done the Application Manager will call
    * startApp to resume. For this example we don't need to do anything.
    */
   protected void pauseApp()
   {
   }

   /**
    * Called by the MID's Application Manager when the MIDlet is about to
    * be destroyed (removed from memory). You should take this as an opportunity
    * to clear up any resources and save the game. For this example we don't
    * need to do anything.
    * @param unconditional if false you have the option of throwing a
    * MIDletStateChangeException to abort the destruction process.
    * @throws MIDletStateChangeException
    */
   protected void destroyApp(boolean unconditional) throws MIDletStateChangeException
   {
   }

   /**
    * This method is called as a result of the item's state be changed by the
    * user (ie. they entered some text). After checking we have the right item
    * we then popup a little alert acknowledging the event.
    * @param item
    */
   public void itemStateChanged(Item item)
   {
      System.out.println("item state changed for " + item);
      if (item == textFieldItem)
      {
         Display.getDisplay(this).setCurrent(
                 new Alert("", "You said  " + textFieldItem.getString(),
                           null, AlertType.INFO));
      }
   }

   /**
    * The CommandListener interface method called when the user executes a
    * Command, in this case it can only be the quit command we created in the
    * consutructor and added to the Form.
    * @param command the command that was triggered
    * @param displayable the displayable on which the event occurred
    */
   public void commandAction(Command command, Displayable displayable)
   {
      try
      {
         if (command == quit)
         {
            destroyApp(true);
            notifyDestroyed();
         }
      }

      catch (MIDletStateChangeException me)
      {
         System.out.println(me + " caught.");
      }
   }
}

⌨️ 快捷键说明

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