Java Tutorial/J2ME/StringItem — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
 
м (1 версия)
 
(нет различий)

Текущая версия на 05:00, 1 июня 2010

Add StringItem to a form

import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.midlet.MIDlet;
public class FormStringItemMIDlet extends MIDlet {
  protected Display display;
  protected void startApp() {
    display = Display.getDisplay(this);
    Form form = new Form("Item Layout");
    form.append("line");
    form.append(new StringItem("State ", "OK"));
    display.setCurrent(form);
  }
  protected void pauseApp() {
  }
  protected void destroyApp(boolean unconditional) {
  }
}





Create Form with a list of StringItems

import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.midlet.MIDlet;
public class J2MECreatingFormWithItems extends MIDlet implements CommandListener {
  private Display display;
  private Form form;
  private Command exit;
  public J2MECreatingFormWithItems() {
    display = Display.getDisplay(this);
    exit = new Command("Exit", Command.SCREEN, 1);
    StringItem messages[] = new StringItem[2];
    messages[0] = new StringItem("Welcome, ", "glad you could come.");
    messages[1] = new StringItem("Hello, ", "Mary.");
    form = new Form("Display Form with Items", messages);
    form.addCommand(exit);
    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 == exit) {
      destroyApp(true);
      notifyDestroyed();
    }
  }
}





Get Label and text from StringItem

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.midlet.MIDlet;
public class StringItemMIDlet extends MIDlet implements CommandListener {
  private Command exitCommand = new Command("Exit", Command.EXIT, 1);
  private Command setLabelCommand = new Command("Label", Command.SCREEN, 1);
  private Command setTextCommand = new Command("Text", Command.SCREEN, 1);
  private Command getLabelTextCommand = new Command("Get", Command.SCREEN, 1);
  private StringItem stringItem1 = new StringItem("Label1", "Content1");
  private StringItem stringItem2 = new StringItem(null, "Content2");
  private Display display = Display.getDisplay(this);
  public StringItemMIDlet() {
  }
  public void startApp() {
    Form aForm = new Form("StringItem");
    aForm.append(stringItem1);
    aForm.append(stringItem2);
    aForm.addCommand(exitCommand);
    aForm.addCommand(setLabelCommand);
    aForm.addCommand(setTextCommand);
    aForm.addCommand(getLabelTextCommand);
    aForm.setCommandListener(this);
    display.setCurrent(aForm);
  }
  public void pauseApp() {
  }
  public void destroyApp(boolean unconditional) {
  }
  public void commandAction(Command c, Displayable s) {
    if (c == exitCommand) {
      destroyApp(false);
      notifyDestroyed();
    }
    if (c == setLabelCommand)
      stringItem1.setLabel("new label");
    if (c == setTextCommand)
      stringItem2.setText("new text");
    if (c == getLabelTextCommand) {
      Alert alert = new Alert("Info", stringItem1.getLabel() + ":" + stringItem1.getText(), null,
          AlertType.INFO);
      alert.setTimeout(5000);
      display.setCurrent(alert);
    }
  }
}





Set new text value to StringItem

import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.midlet.MIDlet;
public class J2MEStringItemExample extends MIDlet implements CommandListener {
  private Display display;
  private Form form = new Form("Quiz");
  private StringItem question = new StringItem("Question: ", "A ?");
  private Command giveup = new Command("Give Up", Command.SCREEN, 1);
  private Command exit = new Command("Exit", Command.EXIT, 1);
  public J2MEStringItemExample() {
    display = Display.getDisplay(this);
    form.addCommand(exit);
    form.addCommand(giveup);
    form.append(question);
    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 == giveup) {
      question.setLabel("Answer: ");
      question.setText("Survivors are not buried.");
      form.removeCommand(giveup);
    } else if (command == exit) {
      destroyApp(false);
      notifyDestroyed();
    }
  }
}





StringItem with line break

import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.midlet.MIDlet;
public class FormStringItemLineBreakMIDlet extends MIDlet {
  protected Display display;
  protected void startApp() {
    display = Display.getDisplay(this);
    Form form = new Form("Item Layout");
    form.append("line");
    form.append(new StringItem(null, "No\n label\n"));
    display.setCurrent(form);
  }
  protected void pauseApp() {
  }
  protected void destroyApp(boolean unconditional) {
  }
}