Java Tutorial/J2ME/Alert

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

Alarm alert, confirmation alert, error alert, info alert, warning alert

   <source lang="java">

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

}</source>





Set alert time out

   <source lang="java">

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

}</source>





Two alerts

   <source lang="java">

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.TextBox; import javax.microedition.lcdui.TextField; import javax.microedition.midlet.MIDlet; public class J2METwoAlerts extends MIDlet implements CommandListener {

 private Display mDisplay;
 private TextBox mTextBox= new TextBox("TwoAlerts", "", 32, TextField.ANY);
 private Alert mTimedAlert = new Alert("Network error", "error occurred", null, AlertType.INFO);
 private Alert mModalAlert= new Alert("About TwoAlerts","Alerts.", null, AlertType.INFO);
 private Command mAboutCommand, mGoCommand, mExitCommand;
 public J2METwoAlerts() {
   mAboutCommand = new Command("About", Command.SCREEN, 1);
   mGoCommand = new Command("Go", Command.SCREEN, 1);
   mExitCommand = new Command("Exit", Command.EXIT, 2);
   mTextBox.addCommand(mAboutCommand);
   mTextBox.addCommand(mGoCommand);
   mTextBox.addCommand(mExitCommand);
   mTextBox.setCommandListener(this);
   mModalAlert.setTimeout(Alert.FOREVER);
 }
 public void startApp() {
   mDisplay = Display.getDisplay(this);
   mDisplay.setCurrent(mTextBox);
 }
 public void pauseApp() {
 }
 public void destroyApp(boolean unconditional) {
 }
 public void commandAction(Command c, Displayable s) {
   if (c == mAboutCommand)
     mDisplay.setCurrent(mModalAlert);
   else if (c == mGoCommand)
     mDisplay.setCurrent(mTimedAlert, mTextBox);
   else if (c == mExitCommand)
     notifyDestroyed();
 }

}</source>