如何在 Java ME 中向用户询问一些号码?

发布于 2024-08-18 18:39:19 字数 91 浏览 7 评论 0原文

我对 Java Me 很陌生(第一次)。我希望我的程序向用户询问 IP 地址。 0 到 255 之间的四个数字。这并不困难,但正如我所说,我是 Java Me 的新手。

I am very new to Java Me (first time). I want my program to ask the user for an IP addres. So four numbers that are between 0 and 255. It doesn't need to be difficult, but as I said, I'm new to Java Me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

看海 2024-08-25 18:39:19

您希望用户如何输入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();
    }
  }
}

How do you want the user to input the IP address? Do you want to use a TextField for example? You could do something like the following:

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();
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文