日期:2014-05-20  浏览次数:20773 次

J2ME学习笔记:Alert控件的使用【原创】

首先我们要知道:Alert是一个显示等待或错误信息的屏幕类,其构造方法如下:Alert(标题,初始内容,图像,类型)如Alert info = new Alert("Alert","This is a Alert Example!",img,AlertType.INFO);

以下给出一个代码:

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


public class Alert extends MIDlet implements CommandListener {

private Command exitCommand;
private Command alertCommand;
private TextBox tb;

public Alert()
{
?? exitCommand = new Command("Exit",Command.EXIT,1);
?? alertCommand = new Command("Alert",Command.SCREEN,1);
?? tb = new TextBox("Terry","Alert Example",15,0);
?? tb.addCommand(exitCommand);
?? tb.addCommand(alertCommand);
?? tb.setCommandListener(this);

?? Display.getDisplay(this).setCurrent(tb);
}
?? public Alert(String string, String string2, Image img, AlertType info) {
?? // TODO Auto-generated constructor stub
}
protected void startApp() throws MIDletStateChangeException
??? {
//?? Display.getDisplay(this).setCurrent(tb);
??
}

protected void destroyApp(boolean arg0) throws MIDletStateChangeException
{
??

}

protected void pauseApp()
{
??

}
public void commandAction(Command c, Displayable d) {
?? //如果触发alertCommand按钮,则显示Alert屏幕
?? if(d == tb && c == alertCommand){
??? Image img;
??? try{
???? img = Image.createImage("/2.png");
??? }
??? catch(java.io.IOException e)
??? {
???? img = null;
???? System.out.println("img is load...");
??? }
??? //创建一个Alert屏幕对象
??? Alert info = new Alert("Alert","This is a Alert Example!",img,AlertType.INFO);
???
?? }
??
??
}

?

}

?