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

📄 confirmdialog.java

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

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

/**
 *
 * @author Liu Bin
 */
public class ConfirmDialog {
    public static final int MR_YES = 0;
    public static final int MR_NO = 1;
    public static final int MR_OK = 2;
    public static final int MR_CANCEL = 3;
    
    private int modalResult = -1;
    
    Display dis;
    Form form;
    Displayable curDisplayable;
    Command cmdOK;
    Command cmdCancel;
    
    public ConfirmDialog(Display display, String title, String text) {
        dis = display;
        form = new Form(title);
        form.append(text);
    }
    
    /**
     * 显示运行对话框,该对话框被执行前不能执行任何操作
     * <p>
     * @return 对话框的哪个按钮被按下了
     */
    public int showDialog() {
        curDisplayable = dis.getCurrent();
        cmdOK = new Command("确定", Command.OK, 1);
        cmdCancel = new Command("取消", Command.CANCEL, 2);
        
        CommandListenerThread clt = new CommandListenerThread();
        form.addCommand(cmdOK);
        form.addCommand(cmdCancel);
        form.setCommandListener(clt); 
        dis.setCurrent(form);
        clt.run();
        dis.setCurrent(curDisplayable);
        return modalResult;
    }
    
    class CommandListenerThread extends Thread implements CommandListener {
        /**
         * 处理命令按钮事件
         */
        public void commandAction(Command c, Displayable s) {
            if (c == cmdOK) {
                modalResult = MR_OK;
            } else if (c == cmdCancel) {
                modalResult = MR_CANCEL;
            }
        }
        
        public void run() {
            while (modalResult<0) {
                try {
                    sleep(100);
                } catch (Exception e) {
                    System.out.println("运行失败:" + e.toString());
                }
            }
        }
    }
    
}

⌨️ 快捷键说明

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