|
/* * @(#)AlertsMIDlet.java */
package zieckey.j2me.study;
import javax.microedition.lcdui.Alert; import javax.microedition.lcdui.AlertType; import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.TextBox; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException;
/** * 该程序用来学习Alert控件的用法。 不同的Alert类型会发出不同的声音。 * * @author Zieckey * @version 07/01/19 */ public class AlertsMIDlet extends MIDlet implements CommandListener { private Display display; private TextBox textbox;
private Command exitCommand; private Command alarmCommand; private Command confirmCommand; private Command errorCommand; private Command infoCommand; private Command warningCommand;
private Alert alarmAlert; private Alert confirmAlert; private Alert errorAlert; private Alert infoAlert; private Alert warningAlert;
/** * 构造函数 */ public AlertsMIDlet( ) { display = Display.getDisplay( this );// 提取系统设备的Display对象
exitCommand = new Command( "Exit", Command.EXIT, 2 );
alarmCommand = new Command( "Alarm", Command.SCREEN, 2 );// 对该菜单的相应应用到当前整个屏幕
confirmCommand = new Command( "Confirm", Command.SCREEN, 2 ); errorCommand = new Command( "Error", Command.SCREEN, 2 ); infoCommand = new Command( "Information", Command.SCREEN, 2 ); warningCommand = new Command( "Warning", Command.SCREEN, 2 );
textbox = new TextBox( "Main UI", "Please click the soft keys to choose a Alert to run....", 256, 0 ); textbox.addCommand( alarmCommand ); textbox.addCommand( confirmCommand ); textbox.addCommand( errorCommand ); textbox.addCommand( infoCommand ); textbox.addCommand( warningCommand ); textbox.addCommand( exitCommand );
textbox.setCommandListener( this );
alarmAlert = new Alert( "Alarm", "This is an \"ALARM\" alert! ", null, AlertType.ALARM ); confirmAlert = new Alert( "Confirmation", "This is an \"CONFIRMATION\" alert! ", null, AlertType.CONFIRMATION ); errorAlert = new Alert( "Error", "This is an \"ERROR\" alert! ", null, AlertType.ERROR ); infoAlert = new Alert( "Info", "This is an \"INFORMATION\" alert! ", null, AlertType.INFO ); warningAlert = new Alert( "Warning", "This is an \"WARNING\" alert! ", null, AlertType.WARNING );
alarmAlert.setTimeout( 3000 ); confirmAlert.setTimeout( 3000 ); errorAlert.setTimeout( 3000 ); infoAlert.setTimeout( 3000 ); warningAlert.setTimeout( 3000 );
display.setCurrent( textbox ); }
protected void startApp() throws MIDletStateChangeException {
}
protected void destroyApp( boolean arg0 ) throws MIDletStateChangeException {
}
protected void pauseApp() {
}
public void commandAction( Command cmd, Displayable arg1 ) { if ( cmd == exitCommand ) { try { destroyApp( true ); } catch ( MIDletStateChangeException e ) { e.printStackTrace( ); } notifyDestroyed( ); } else if ( cmd == infoCommand ) { display.setCurrent( infoAlert ); } else if ( cmd == errorCommand ) { display.setCurrent( errorAlert ); } else if ( cmd == alarmCommand ) { display.setCurrent( alarmAlert ); } else if ( cmd == confirmCommand ) { display.setCurrent( confirmAlert ); } else if ( cmd == warningCommand ) { display.setCurrent( warningAlert ); } } }
|