stringitemmidlet.java

来自「<j2me 开发精解> 詹建光著 里所有的源码。对J2me的开发相当」· Java 代码 · 共 62 行

JAVA
62
字号
package com.j2medev.sample.chapter3;

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

public class StringItemMidlet extends MIDlet implements ItemCommandListener,CommandListener {
    
    private Form form = new Form("UIDemo");
    private Display display = null;
    private StringItem s1,s2,s3;
    private Command exitCommand = new Command("退出",Command.EXIT,1);
    private Command buttonCommand = new Command("按钮模式",Command.ITEM,2);
    private Command linkCommand = new Command("链接模式",Command.ITEM,3);
    
    public void startApp() {
        if(display==null) {
            display = Display.getDisplay(this);
            s1 = new StringItem("1", "普通文本", StringItem.PLAIN);
            s2 = new StringItem("2", "按钮模式", StringItem.BUTTON);
            s3 = new StringItem("3", "超级链接", StringItem.HYPERLINK);
            //为BUTTON和HYPERLINK模式的StringItem设置默认Command
            s2.setDefaultCommand(buttonCommand);
            s3.setDefaultCommand(linkCommand);
            //注册ItemCommandListener监听器
            s2.setItemCommandListener(this);
            s3.setItemCommandListener(this);
            form.append(s1);
            form.append(s2);
            form.append(s3);
            form.addCommand(exitCommand);
            form.setCommandListener(this);
        }
        display.setCurrent(form);
    }
    
    public void pauseApp() {
    }
    
    public void destroyApp(boolean unconditional) {
    }
    
    public void commandAction(Command cmd,Displayable displayable){
        if(cmd == exitCommand){
            destroyApp(false);
            notifyDestroyed();
        }
    }
    //当特定Item上的Command被按下的时候此方法就会被调用
    public void commandAction(Command cmd, Item item) {
        if(cmd == buttonCommand){
            if(item == s2){
                System.out.println("按钮模式的StringItem被选中");
            }
        }else if(cmd == linkCommand){
            if(item == s3){
                System.out.println("超级链接模式的StringItem被选中");
            }
        }
    }
    
}

⌨️ 快捷键说明

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