(17)—–textbox文本框对象
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值,那
么第三个参数可以任意设。
showtextbox.java的运行效果如下图所示:(缺)
(18)—–textfield文本域对象
textfield和textbox有点相似,不过textbox是多行的,而textfield是单行的。而且textbo
x是容器类型的对象,但是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类的构造函数,第一个参数是文本域的名称
,第二个参数是缺省值,第三个参数是长度,第四个参数是文本域的类型,可选的值有: textfi
eld.password、textfield.emailaddr、textfield.phonenumber、textfield. url、textfi
eld. numeric等等。构造好textfield对象之后,调用form的append()方法将它添加到form对
象的子项目中。showtextfield.java程序的运行效果如下图所示:(缺)
(19)—–datefield对象
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程序的运行效果如下面两图所示:(缺)
(20)—–stringitem对象
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;
}
}
showstringitem.java程序的运行效果如下图所示:(缺)
21)—–imageitem对象
imageitem对象是一个项目类型的对象,他的作用是在容器中显示图片。那么如何使用imag
eitem对象呢?请按照下面三个步骤进行:
1.构造一个image对象,相关代码如下所示:
image img=image.createimage("/fancy/test/javapowered-8.png");
createimage()方法是image类的静态方法,它的作用是根据图形文件创建一个image对象。
j2me程序中所用到的图片文件必须存放在apps\fancy\res文件夹下面。
2.构造imageitem对象,相关代码如下所示:
imgitem=new imageitem("default layout",img,imageitem.layout_default,
"image cannot be shown");
imageitem类的构造函数有三个参数,第一个参数的作用是显示一个标签,第二个参数指明图
片的对齐方式,第三个参数的作用是显示图片的tip。
3.利用容器类对象的append()方法将imageitem对象添加进去。如:
props.append(imgitem);
下面我们来看一个比较完整的例子。
package fancy.test;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class showimageitem extends midlet implements commandlistener
{
private display display;
private form props;
private image img;
private imageitem imgitem;
private command exitcommand = new command("exit", command.exit, 1);
public showimageitem()
{
display = display.getdisplay(this);
}
public void startapp()
{
props = new form("hello world");
//props.append("hello world!\n");
try
{
img=image.createimage("/fancy/test/javapowered-8.png");
imgitem=new imageitem("default layout",
img,imageitem.layout_default,"image cannot be shown");
props.append(imgitem);
props.append(new imageitem("left layout",
img,imageitem.layout_left,"image cannot be shown"));
props.append(new imageitem("center layout",
img,imageitem.layout_center,"image cannot be shown"));
}
catch(exception fe)
{
//to do nothing
}
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;
}
}
showimageitem.java程序的运行效果如下图所示:(缺)
————————————————————————————
网易广州社区java版
