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

📄 microdialog.java

📁 J2ME程序设计实例教程的源码
💻 JAVA
字号:
import javax.microedition.lcdui.*;

/**
 * 该类实现了对话框的功能
 */
public class MicroDialog extends MicroComponent 
                            implements CommandListener {
    //
    public static final int YES_NO = 0;
    public static final int OK = 1;
    public static final int OK_CANCEL = 2;
    public static final int YES = 3;
    public static final int NO = 4;
    public static final int CANCEL = 5;
    
    private static final String YES_LABEL = "是";
    private static final String NO_LABEL = "否";
    private static final String OK_LABEL = "确定";
    private static final String CANCEL_LABEL = "取消";
    
    private String message; //对话框显示的消息
    private String title;   //对话框标题
    private int type;       //对话框的类型
    private int action = -1 ;     //用户响应的动作
    
    private MicroDialogListener mdListener; //对话框监视器
    private TextBox screen;
    
    public MicroDialog(Display display, int type) {
        super(display);
        this.title = "对话框";
        this.type = type;
    }
    
    public void setMessage(String message) {
        this.message = message;
    }
    
    public void setTitle(String title) {
        this.title = title;
    }
    
    //注册对话框监视器
    public void setMicroDialogListener(MicroDialogListener mdListener) {
        this.mdListener = mdListener;
    }
    
    protected void createModel() {
        if(title == null) {
            title = "对话框";
        }
        if(message == null) {
            message = "";
        }
    }
    
    protected void createGUI() {
        screen = new TextBox(title, message, 256, TextField.UNEDITABLE);
        if(type == MicroDialog.YES_NO) {
            Command cmdYes = new Command(MicroDialog.YES_LABEL, Command.SCREEN, 1);
            Command cmdNo = new Command(MicroDialog.NO_LABEL, Command.SCREEN, 1);
            screen.addCommand(cmdYes);
            screen.addCommand(cmdNo);
        }
        else if(type == MicroDialog.OK) {
            Command cmdOk = new Command(MicroDialog.OK_LABEL, Command.OK, 1);
            screen.addCommand(cmdOk);
        }
        else if(type == MicroDialog.OK_CANCEL) {
            Command cmdOk = new Command(MicroDialog.OK_LABEL, Command.OK, 1);
            Command cmdCancel = new Command(MicroDialog.CANCEL_LABEL, Command.CANCEL, 1);
            screen.addCommand(cmdOk);
            screen.addCommand(cmdCancel);
        }
        screen.setCommandListener(this);
    }
    
    protected Displayable getGUI() {
        return screen;
    }
    
    protected void update() {
        screen.setTitle(title);
        screen.setString(message);
    }
    
    public void commandAction(Command cmd, Displayable displayable) {
        int actionCode = -1;
        if(cmd.getLabel().equals(MicroDialog.YES_LABEL)) {
            actionCode = MicroDialog.YES;
        }
        else if(cmd.getLabel().equals(MicroDialog.NO_LABEL)) {
            actionCode = MicroDialog.NO;
        }
        else if(cmd.getLabel().equals(MicroDialog.OK_LABEL)) {
            actionCode = MicroDialog.OK;
        }
        else if(cmd.getLabel().equals(MicroDialog.CANCEL_LABEL)) {
            actionCode = MicroDialog.CANCEL;
        }
        if(mdListener != null) {
            mdListener.action(this, actionCode);
        }
    }
}

⌨️ 快捷键说明

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