TextBox是一个容器类型的对象(和Form的性质一样)。用法如下所示:
package fancy.test;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class ShowTextBox extends MIDlet implements CommandListener
{
private Display display;
private TextBox txtBox;
private Command exitCommand = new Command("Exit", Command.EXIT, 1);
public ShowTextBox()
{
display = Display.getDisplay(this);
}
public void startApp()
{
//or :
//String str="hello world";
//txtBox = new TextBox("Text Box",str,str.length(),0);
//the follow code is wrong:
//txtBox = new TextBox("Text Box",str,any number here,0);
txtBox = new TextBox("Text Box",null,200,0);
txtBox.addCommand(exitCommand);
txtBox.setCommandListener(this);
display.setCurrent(txtBox);
}
public void commandAction(Command c, Displayable s)
{
if (c == exitCommand)
{
destroyApp(false);
notifyDestroyed();
}
}
public void destroyApp(boolean unconditional)
{
}
public void pauseApp()
{
display.setCurrent(null);
txtBox = null;
}
}
请注意TextBox类的构造函数,第一个参数实际上是窗口的名称(因为TextBox是一个容器,可能是当前屏幕的显示对象),第二个参数是缺省值,第三个参数是输入字符的总长度。如果你设置了文本框的缺省值,那么第三个参数必须是缺省字符的长度。如果第三个参数的值和缺省字符的长度不一样,那么程序运行不成功(编译可以通过)。如果你将第二个参数置为null值,那么第三个参数可以任意设。
TextField和TextBox有点相似,不过TextBox是多行的,而TextField是单行的。而且TextBox是容器类型的对象,但是TextField是项目类型的对象,只能够被容器包含,不能够单独显示。
TextField文本域对象的用法如下所示:
package fancy.test;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class ShowTextField extends MIDlet implements CommandListener
{
private Display display;
private Form props;
private TextField txtField;
private Command exitCommand = new Command("Exit", Command.EXIT, 1);
public ShowTextField()
{
display = Display.getDisplay(this);
}
public void startApp()
{
props = new Form("Hello World");
props.append("Hello World!\n");
txtField=new TextField("EMail:", "", 15,TextField.EMAILADDR);
props.append(txtField);
props.addCommand(exitCommand);
props.setCommandListener(this);
display.setCurrent(props);
}
public void commandAction(Command c, Displayable s)
{
if (c == exitCommand)
{
destroyApp(false);
notifyDestroyed();
}
}
public void destroyApp(boolean unconditional)
{
}
public void pauseApp()
{
display.setCurrent(null);
props = null;
}
}
请注意startApp()方法,我们使用Form对象作为当前屏幕的显示对象,而将
TextField对象作为Form的一个子项目显示。下面来介绍TextField类的构造函数,第一个参数是文本域的名称,第二个参数是缺省值,第三个参数是长度,第四个参数是文本域的类型,可选的值有:
TextField.PASSWORD、TextField.EMAILADDR、TextField.PHONENUMBER、TextField.
URL、TextField. NUMERIC等等。构造好TextField对象之后,调用Form的append()方法将它添加到Form对象的子项目中。ShowTextField.java程序的运行效果如下图所示:
DateField对象和TextField对象一样同属于项目类型的对象,不能够单独显示,必须作为容器对象的子项目显示。DateField对象的作用是显示一个日期,它和Windows控制面板中的时间和日期设置程序有点近似。DateField对象的用法如下所示:
package fancy.test;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class ShowDateField extends MIDlet implements CommandListener
{
private Display display;
private Form props;
private DateField datField;
private Command exitCommand = new Command("Exit", Command.EXIT, 1);
public ShowDateField()
{
display = Display.getDisplay(this);
}
public void startApp()
{
props = new Form("Hello World");
props.append("Hello World!\n");
//change:
//datField=new DateField("Date:",DateField.DATE_TIME);
//datField=new DateField("Date:",DateField.TIME);
datField=new DateField("Date:",DateField.DATE);
props.append(datField);
props.addCommand(exitCommand);
props.setCommandListener(this);
display.setCurrent(props);
}
public void commandAction(Command c, Displayable s)
{
if (c == exitCommand)
{
destroyApp(false);
notifyDestroyed();
}
}
public void destroyApp(boolean unconditional)
{
}
public void pauseApp()
{
display.setCurrent(null);
props = null;
}
}
ShowDateField.java程序的运行效果如下面两图所示:
StringItem对象和TextField、DateField对象类似,同样属于项目类型的对象。它的作用就是在容器对象中显示一条字符串。
package fancy.test;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class ShowStringItem extends MIDlet implements CommandListener
{
private Display display;
private Form props;
private StringItem strItem;
private StringItem strItem2;
private Command exitCommand = new Command("Exit", Command.EXIT, 1);
public ShowStringItem()
{
display = Display.getDisplay(this);
}
public void startApp()
{
props = new Form("Hello World");
props.append("Hello World!\n");
strItem=new StringItem("signature:","小楼一夜听春雨");
strItem2=new StringItem("signature:","三教明天考物化");
props.append(strItem);
props.append(strItem2);
props.addCommand(exitCommand);
props.setCommandListener(this);
display.setCurrent(props);
}
public void commandAction(Command c, Displayable s)
{
if (c == exitCommand)
{
destroyApp(false);
notifyDestroyed();
}
}
public void destroyApp(boolean unconditional)
{
}
public void pauseApp()
{
display.setCurrent(null);
props = null;
} <!--v:3.2-->