⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 alerttypedemo.java

📁 人民邮电出版社的《J2ME手机开发入门》全部源代码
💻 JAVA
字号:
/*
 * AlertTypeDemo.java
 *
 * Created on 2005年3月7日, 上午10:03
 */

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
 *
 * @author  Liu Bin
 * @version
 */
public class AlertTypeDemo extends MIDlet
        implements CommandListener {
    
    private Display display;
    
    private TextBox tb = new TextBox("Alert & AlertType演示",
            "", 500, TextField.ANY);
    
    /** 退出命令按钮. */
    private Command cmdExit;
    /** 无模式警告按钮. */
    private Command cmdAlarm;
    /** 模式警告按钮. */
    private Command cmdAlarmModal;
    /** 无模式确认按钮. */
    private Command cmdConfirmation;
    /** 模式确认按钮. */
    private Command cmdConfirmationMoal;
    /** 无模式错误按钮. */
    private Command cmdError;
    /** 模式错误按钮. */
    private Command cmdErrorModal;
    /** 无模式信息按钮. */
    private Command cmdInfo;
    /** 模式信息按钮. */
    private Command cmdInfoModal;
    /** 无模式提醒按钮. */
    private Command cmdWarning;
    /** 模式提醒按钮. */
    private Command cmdWarningModal;
    
    //定义超时常量
    private static final int TIMEOUT = 3;
    
    //构造函数
    public AlertTypeDemo() {
        cmdExit = new Command("Exit", Command.EXIT,1);
        cmdAlarm = new Command("上无模式警告", Command.SCREEN,2);
        cmdAlarmModal = new Command("模式警告", Command.SCREEN,2);
        cmdConfirmation = new Command("无模式确认", Command.SCREEN,2);
        cmdConfirmationMoal = new Command("模式确认", Command.SCREEN,2);
        cmdError = new Command("无模式错误", Command.SCREEN,2);
        cmdErrorModal = new Command("模式错误", Command.SCREEN,2);
        cmdInfo = new Command("无模式信息", Command.SCREEN,2);
        cmdInfoModal = new Command("模式信息", Command.SCREEN,2);
        cmdWarning = new Command("无模式提醒", Command.SCREEN,2);
        cmdWarningModal = new Command("模式提醒", Command.SCREEN,2);
        
        tb.addCommand(cmdExit);
        tb.addCommand(cmdAlarm);
        tb.addCommand(cmdAlarmModal);
        tb.addCommand(cmdConfirmation);
        tb.addCommand(cmdConfirmationMoal);
        tb.addCommand(cmdError);
        tb.addCommand(cmdErrorModal);
        tb.addCommand(cmdInfo);
        tb.addCommand(cmdInfoModal);
        tb.addCommand(cmdWarning);
        tb.addCommand(cmdWarningModal);
        
        tb.setCommandListener(this);
    }
    
    public void startApp() {
        display = Display.getDisplay(this);
        display.setCurrent(tb);
    }
    
    public void pauseApp() {
    }
    
    public void destroyApp(boolean unconditional) {
        notifyDestroyed();
    }
    
    /**
     * 处理命令按钮事件
     */
    public void commandAction(Command cmd, Displayable d) {
        if (cmd == cmdExit) {
            destroyApp(true);
        } else if (cmd == cmdAlarm) {
            displayAlert("无模式警告",
                    "这里显示无模式警告的内容,在超时时间过后会自动退出",
                    "alarm",
                    AlertType.ALARM,
                    TIMEOUT);
        }  else if (cmd == cmdAlarmModal) {
            displayAlert("模式警告",
                    "这里显示模式警告的内容," +
                    "该屏幕会永远存在直到用户主动安完成(Done)键退出",
                    "alarm",
                    AlertType.ALARM,
                    Alert.FOREVER);
        } else if (cmd == cmdConfirmation) {
            displayAlert("无模式确认",
                    "这里显示无模式确认的内容,在超时时间过后会自动退出",
                    "confirmation",
                    AlertType.CONFIRMATION,
                    TIMEOUT);
        } else if (cmd == cmdConfirmationMoal) {
            displayAlert("模式确认",
                    "这里显示模式确认的内容," +
                    "该屏幕会永远存在直到用户主动安完成(Done)键退出",
                    "confirmation",
                    AlertType.CONFIRMATION,
                    Alert.FOREVER);
        } else if (cmd == cmdError) {
            displayAlert("无模式错误",
                    "这里显示无模式错误的内容,在超时时间过后会自动退出",
                    "error",
                    AlertType.ERROR,
                    TIMEOUT);
        } else if (cmd == cmdErrorModal) {
            displayAlert("模式错误",
                    "这里显示模式错误的内容," +
                    "该屏幕会永远存在直到用户主动安完成(Done)键退出",
                    "error",
                    AlertType.ERROR,
                    Alert.FOREVER);
        } else if (cmd == cmdInfo) {
            displayAlert("无式信息",
                    "这里显示无式信息认的内容,在超时时间过后会自动退出",
                    "information",
                    AlertType.INFO,
                    TIMEOUT);
        } else if (cmd == cmdInfoModal) {
            displayAlert("式信息",
                    "这里显示式信息的内容," +
                    "该屏幕会永远存在直到用户主动安完成(Done)键退出",
                    "information",
                    AlertType.INFO,
                    Alert.FOREVER);
        } else if (cmd == cmdWarning) {
            displayAlert("无模式提醒",
                    "这里显示无模式提醒的内容,在超时时间过后会自动退出",
                    "warning",
                    AlertType.WARNING,
                    TIMEOUT);
        } else if (cmd == cmdWarningModal) {
            displayAlert("模式提醒",
                    "这里显示模式提醒的内容," +
                    "该屏幕会永远存在直到用户主动安完成(Done)键退出",
                    "warning",
                    AlertType.WARNING,
                    Alert.FOREVER);
        }
    }
    
    /**
     * 根据不同的参数显示不同类型的Alert对话框.
     * <p>
     * @param title Alert的标签
     * @param alertText 在Alert中显示的文字
     * @param imageFile 在Alert中显示的图像的名字
     * @param type AlertType类型变量,指定了Alert的显示类型
     * @param timeout Alert对话框的超时时间
     */
    private void displayAlert(String title, String alertText,
            String imageFile, AlertType type, int timeout) {
        Alert alert;
        Image alertImage = null;
        
        try {
            alertImage = Image.createImage("/" + imageFile + ".png");
        } catch(java.io.IOException err) {
        }
        //创建Alert
        alert = new Alert(title,alertText,alertImage,type);
        
        //设置超时
        if (timeout != Alert.FOREVER) {
            alert.setTimeout(timeout * 1000);
        } else {
            alert.setTimeout(Alert.FOREVER);
        }
        display.setCurrent(alert,tb);
        tb.setString(tb.getString() + alertText + "\n\n");
        if (!type.playSound(display)) {
            tb.setString(tb.getString() + "播放声音错误\n");
        }
    }
}

⌨️ 快捷键说明

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