簡體   English   中英

如何在Java ME中向用戶詢問一些數字?

[英]How can I ask the user for some numbers in Java ME?

我是Java Me的新手(第一次)。 我希望我的程序向用戶詢問IP地址。 因此,四個數字介於0到255之間。這並不困難,但是正如我所說,我是Java Me的新手。

您希望用戶如何輸入IP地址? 您是否要使用TextField例如? 您可以執行以下操作:

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

public class GetIPAddress extends MIDlet implements CommandListener {

  private Display display;
  private Form form = new Form("IP Adress Input");
  private Command submit = new Command("Submit", Command.SCREEN, 1);
  private Command exit = new Command("Exit", Command.EXIT, 1);

  private TextField textfield = new TextField("IP Address:", "", 30, TextField.ANY);

  public GetIPAddress() {
    display = Display.getDisplay(this);
    form.addCommand(exit);
    form.addCommand(submit);
    form.append(textfield);
    form.setCommandListener(this);
  }

  public void startApp() {
    display.setCurrent(form);
  }

  public void pauseApp() {
  }

  public void destroyApp(boolean unconditional) {
  }

  public void commandAction(Command command, Displayable displayable) {
    if (command == submit) {
      // Do the manipulation of the IP address here. 
      // You can retrieve it using textfield.getString();
      form.removeCommand(submit);
    } else if (command == exit) {
      destroyApp(false);
      notifyDestroyed();
    }
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM