Java by API/javax.microedition.lcdui/AlertType

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

AlertType.CONFIRMATION

 

import java.io.IOException;
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.Image;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
public class FewAlertsMIDlet extends MIDlet implements CommandListener {
  private Command exitCommand = new Command("exit", Command.SCREEN, 1);
  private Command alarmCommand = new Command("alarm", Command.SCREEN, 2);
  private Command confirmCommand= new Command("confirm", Command.SCREEN, 2);
  private Command errorCommand = new Command("error", Command.SCREEN, 2);
  private Command infoCommand = new Command("info", Command.SCREEN, 2);
  private Command warningCommand = new Command("warning", Command.SCREEN, 2);
  private Alert alarmAlert = new Alert("alarm",null,null,  AlertType.ALARM);
  private Alert confirmAlert = new Alert("confirm", null,null, AlertType.CONFIRMATION);
  private Alert errorAlert = new Alert("error", null, null,AlertType.ERROR);
//  private Alert infoAlert = new Alert("info", null,infoImage, AlertType.INFO);
  private Alert warningAlert = new Alert("warning",null, null, AlertType.WARNING);
  private Display display = Display.getDisplay(this);
  public FewAlertsMIDlet() {
  }
  public void startApp() {
    TextBox main = new TextBox("","",26,TextField.ANY);
    Image infoImage = null;
    try {
      infoImage = Image.createImage("/a.png");
    } catch (IOException e) {
    }
    alarmAlert.setTimeout(Alert.FOREVER);
    confirmAlert.setTimeout(3000);
    errorAlert.setTimeout(3000);
  //  infoAlert.setTimeout(3000);
    warningAlert.setTimeout(3000);
    main.addCommand(exitCommand);
    main.addCommand(alarmCommand);
    main.addCommand(confirmCommand);
    main.addCommand(errorCommand);
    main.addCommand(infoCommand);
    main.addCommand(warningCommand);
    main.setCommandListener(this);
    display.setCurrent(main);
  }
  public void pauseApp() {
  }
  public void destroyApp(boolean unconditional) {
  }
  public void commandAction(Command c, Displayable s) {
    if (c == exitCommand) {
      destroyApp(false);
      notifyDestroyed();
    }
    if (c == alarmCommand) {
      display.setCurrent(alarmAlert);
    }
    if (c == confirmCommand) {
      display.setCurrent(confirmAlert);
    }
    if (c == errorCommand) {
      display.setCurrent(errorAlert);
    }
 //   if (c == infoCommand) {
   //   display.setCurrent(infoAlert);
   // }
    if (c == warningCommand) {
      display.setCurrent(warningAlert);
    }
  }
}





AlertType.INFO

   
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);
    }
  }
}





AlertType.WARNING

 

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.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class J2MEDisplayAlert extends MIDlet implements CommandListener {
  private Display display;
  private Alert alert;
  private Form form;
  private Command exit;
  private boolean exitFlag;
  public J2MEDisplayAlert() {
    exitFlag = false;
    display = Display.getDisplay(this);
    exit = new Command("Exit", Command.SCREEN, 1);
    form = new Form("Throw Exception");
    form.addCommand(exit);
    form.setCommandListener(this);
  }
  public void startApp() {
    display.setCurrent(form);
  }
  public void pauseApp() {
  }
  public void destroyApp(boolean unconditional) throws MIDletStateChangeException {
    if (unconditional == false) {
      throw new MIDletStateChangeException();
    }
  }
  public void commandAction(Command command, Displayable displayable) {
    if (command == exit) {
      try {
        if (exitFlag == false) {
          alert = new Alert("Busy", "Please try again.", null, AlertType.WARNING);
          alert.setTimeout(Alert.FOREVER);
          display.setCurrent(alert, form);
          destroyApp(false);
        } else {
          destroyApp(true);
          notifyDestroyed();
        }
      } catch (MIDletStateChangeException exception) {
        exitFlag = true;
      }
    }
  }
}