alertdemo.java

来自「J2ME程序设计实例教程的源码」· Java 代码 · 共 54 行

JAVA
54
字号
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.IOException;

public class AlertDemo extends MIDlet implements CommandListener {
    private Display display;
    private Command cmdOK = new Command("确定", Command.OK, 1);
    private Command cmdExit = new Command("退出", Command.EXIT, 1);
     
    public void startApp() {
        TextBox password = new TextBox("请输入密码", "", 10, 
                            TextField.PASSWORD|TextField.ANY);
        password.addCommand(cmdOK);
        password.addCommand(cmdExit);
        password.setCommandListener(this);
        display = Display.getDisplay(this);
        display.setCurrent(password);
    }
    
    public void pauseApp() {
    }
    
    public void destroyApp(boolean unconditional) {
    }
    
    public void commandAction(Command cmd, Displayable d) {
        if(cmd == cmdOK) {
            String psw = ((TextBox)d).getString();
            if(psw.equals("Yang Guang")) {
                Image img = null;
                try {
                    img = Image.createImage("/ok.png");
                }
                catch(IOException ioe) {
                    //
                }
                Alert alert = new Alert("提示", "您输入的密码正确", 
                                        img, AlertType.INFO);
                alert.setTimeout(Alert.FOREVER);
                display.setCurrent(alert, display.getCurrent());
            }
            else {
                Alert alert = new Alert("错误!", 
                        "您输入的密码: \" " + psw + "\"错误", 
                                null, AlertType.ERROR);
                alert.setTimeout(2000); //延迟2秒
                display.setCurrent(alert, display.getCurrent());
            }
        }
        else if(cmd == cmdExit) {
            notifyDestroyed();
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?