stringitemdemo.java

来自「j2me学习 简单例子」· Java 代码 · 共 121 行

JAVA
121
字号
package example.demoseniorui.stringitem;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemCommandListener;
import javax.microedition.lcdui.StringItem;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

/**
 * <p>J2ME StringItem Demo</p>
 * 
 * @author 刘光辉
 * @version 2009.02.01
 */
public class StringItemDemo extends MIDlet 
	implements CommandListener,ItemCommandListener{

	private static final Command CMD_GO = new Command("Go",Command.ITEM,1);
	private static final Command CMD_PRESS = new Command("Press",Command.ITEM,1);
	private static final Command CMD_EXIT = new Command("Exit",Command.EXIT,1);
	private Display display;
	private Form mainForm;

	/**
	 * <p>StringItemDemo的构造函数</p>
	 */
	public StringItemDemo() {
		display = Display.getDisplay(this);
	}
	
	/**
	 * <p>启动程序,使MIDlet处于active状态</p>
	 * <p>在这个方法里声明了一个Form,用来放置StringItem</p>
	 * <ul>StringItem有两个构造函数,最常见的是需要三个参数的</ul>
	 * @see javax.microedition.midlet.MIDlet#startApp()
	 */
	protected void startApp() throws MIDletStateChangeException {
		mainForm = new Form("String Item Demo");
		mainForm.append("This is a simple label");
		
		StringItem item = new StringItem("This is a StringItem label: ","This is the StringItems text");
		mainForm.append(item);
		
		item = new StringItem("Plain: ", "plain", Item.PLAIN);
		mainForm.append(item);
		
		item = new StringItem("Hyper-Link: ", "hyperlink", Item.HYPERLINK);
		item.setDefaultCommand(CMD_GO);
		item.setItemCommandListener(this);
		mainForm.append(item);
		
		item = new StringItem("Button: ", "Button", Item.BUTTON);
		item.setDefaultCommand(CMD_PRESS);
		item.setItemCommandListener(this);
		mainForm.append(item);
		
		mainForm.addCommand(CMD_EXIT);
		mainForm.setCommandListener(this);
		display.setCurrent(mainForm);
		
	}
	
	/**
	 * <p>暂停程序,使MIDlet处于pause状态</p>
	 * @see javax.microedition.midlet.MIDlet#pauseApp()
	 */
	protected void pauseApp() {

	}
	
	/**
	 * <p>销毁程序,使MIDlet处于destroyed状态</p>
	 * 
	 * @param boolean unconditional 
	 * <ul>true 释放资源保存数据进入destroyed状态,false 抛出异常保持当前状态</ul>
	 * @see javax.microedition.midlet.MIDlet#destroyApp(boolean)
	 */
	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {

	}

	/**
	 * <p>命令处理函数,实现ItemCommandListener接口</p>
	 * @param Command cmd 命令对象
	 * @param Item Item 被放置到显示屏显示的Item对象
	 */
	public void commandAction(Command cmd, Item item) {
		if(cmd == CMD_GO){
			String text = "Go to the URL ...";
			Alert a = new Alert("URL", text , null , AlertType.INFO);
			display.setCurrent(a);
		}else if(cmd == CMD_PRESS){
			String text = "Do an action ...";
			Alert a = new Alert("Action", text , null , AlertType.INFO);
			display.setCurrent(a);
		}
	}

	/**
	 * <p>命令处理函数,实现commandListener接口</p>
	 * @param Command cmd 命令对象
	 * @param Displayable d 被放置到显示屏显示的对象
	 */
    public void commandAction(Command cmd, Displayable d) {
        try {
			destroyApp(false);
		} catch (MIDletStateChangeException e) {
			e.printStackTrace();
		}
        notifyDestroyed();
    }

}

⌨️ 快捷键说明

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