Java by API/javax.microedition.lcdui/Form — различия между версиями

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

Версия 17:43, 31 мая 2010

Form: addCommand(Command cmd)

  
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 Main 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 Main() {
  }
  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);
    }
  }
}





Form: append(Item item)

  
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
public class Main extends MIDlet {
  protected Display display;
  protected boolean started;
  protected void startApp() {
    if (!started) {
      display = Display.getDisplay(this);
      Form form = new Form("Item Layout");
      form.append("line");
      form.append(new TextField("Name", "J. Doe", 32, TextField.ANY));
      form.append("Address");
      form.append(new TextField(null, null, 32, TextField.ANY));
      display.setCurrent(form);
      started = true;
    }
  }
  protected void pauseApp() {
  }
  protected void destroyApp(boolean unconditional) {
  }
}





Form: setItemStateListener(ItemStateListener iListener)

  
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Gauge;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.lcdui.StringItem;
import javax.microedition.midlet.MIDlet;
public class Main extends MIDlet implements ItemStateListener {
  private Gauge mGauge;
  private StringItem mStringItem;
  public Main() {
    int initialValue = 5;
    mGauge = new Gauge("GaugeTitle", true, 10, initialValue);
    mStringItem = new StringItem(null, "[value]");
    itemStateChanged(mGauge);
  }
  public void itemStateChanged(Item item) {
    if (item == mGauge)
      mStringItem.setText("Value = " + mGauge.getValue());
  }
  public void startApp() {
    Form form = new Form("GaugeTracker");
    form.append(mGauge);
    form.append(mStringItem);
    form.setItemStateListener(this);
    Display.getDisplay(this).setCurrent(form);
  }
  public void pauseApp() {
  }
  public void destroyApp(boolean unconditional) {
  }
}





new Form(String value)

  
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
public class Main extends MIDlet {
  protected Display display;
  protected boolean started;
  protected void startApp() {
    if (!started) {
      display = Display.getDisplay(this);
      Form form = new Form("Item Layout");
      form.append("line");
      form.append(new TextField("Name", "J. Doe", 32, TextField.ANY));
      form.append("Address");
      form.append(new TextField(null, null, 32, TextField.ANY));
      display.setCurrent(form);
      started = true;
    }
  }
  protected void pauseApp() {
  }
  protected void destroyApp(boolean unconditional) {
  }
}