(22)—–choicegroup对象
choicegroup也是一个项目类型的对象,它代表一个选择列表,它的作用和list对象类似,不
过后者是一个容器,而前者是一个项目。
我们需要特别注意choicegroup类的构造函数,它有四个参数,第一个参数是标签,第二个参
数是此选择列表的类型,例如多选还是单选。第三个参数是一个字符串数组,代表每个选项的
标签,第四个选项是一个image类型的数组,代表每个选项前面的小图标。下面是一个比较完整
的例子。
package fancy.test;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class showchoicegroup extends midlet implements commandlistener
{
private display display;
private form props;
private image duke;
private image[] imagearray;
private choicegroup choice;
private command exitcommand = new command("exit", command.exit, 1);
public showchoicegroup()
{
display = display.getdisplay(this);
}
public void startapp()
{
props = new form("hello world");
//props.append("hello world!\n");
try
{
image duke= image.createimage("/fancy/test/icon.png");
imagearray = new image[]{duke,duke,duke};
string[] stringarray = { "option a", "option b",
"option c" };
choice=new choicegroup("choice group",
choicegroup.multiple,stringarray,imagearray);
props.append(choice);
}
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;
}
}
showchoicegroup.java程序的运行效果如下图所示:(缺)
(23)—–gauge对象
gauge对象是一个项目类型的对象,它的作用是显示一个进度条。请看下面的源代码。gaug
e类的构造函数的后面两个参数分别是进度条的最大值和初始值。
package fancy.test;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class showgauge extends midlet implements commandlistener
{
private display display;
private form props;
private command exitcommand = new command("exit", command.exit, 1);
public showgauge()
{
display = display.getdisplay(this);
}
public void startapp()
{
props = new form("hello world");
//props.append("hello world!\n");
gauge gauge=new gauge("show gauge",true,100,50);
props.append(gauge);
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;
}
}
showgauge.java程序的运行效果如下图所示:(缺)
(24)—–ticker对象
ticker对象是一个项目类型的对象,它的作用相当于一个滚动消息栏,在屏幕的上方显示滚
动的信息。 ticker类的构造函数仅有一个参数,那就是需要滚动显示的消息。
package fancy.test;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class showticker extends midlet implements commandlistener
{
private display display;
private form props;
private command exitcommand = new command("exit", command.exit, 1);
public showticker()
{
display = display.getdisplay(this);
}
public void startapp()
{
props = new form("hello world");
props.append("hello world!\n");
ticker ticker=new ticker("d??¥ò?ò1
;ìy′oóê");
props.setticker(ticker);
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;
}
}
showticker.java程序的运行效果如下图所示:
25)—-获取文本框的值
在前面的例子中,我们已经演示了如何构造j2me程序的用户界面。现在有一个问题,那就是
如何与用户界面交互呢?亦即如何获取用户通过用户界面输入的值呢?请看下面的例子。
package fancy.test;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class gettextboxvalues extends midlet implements commandlistener
{
private display display;
private textbox txtbox;
private command exitcommand = new command("exit", command.exit, 1);
private command getcommand = new command("getvalues", command.ok, 1);
public gettextboxvalues()
{
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.addcommand(getcommand);
txtbox.setcommandlistener(this);
display.setcurrent(txtbox);
}
public void valuesscreen()
{
form props=new form("get text box values");
props.append(txtbox.getstring());
props.addcommand(exitcommand);
props.setcommandlistener(this);
display.setcurrent(props);
}
public void commandaction(command c, displayable s)
{
if (c == exitcommand)
{
destroyapp(false);
notifydestroyed();
}
if(c==getcommand)
{
valuesscreen();
}
}
public void destroyapp(boolean unconditional)
{
}
public void pauseapp()
{
display.setcurrent(null);
txtbox = null;
}
}
在上面的例子中(gettextboxvalues.java),当我们往文本框中输入文本,并按下退出按钮,接
着选择getvalues命令的时候,将会调用valuesscreen()方法。valuesscreen()方法的源代码如下
:
public void valuesscreen()
{
form props=new form("get text box values");
props.append(txtbox.getstring());
props.addcommand(exitcommand);
props.setcommandlistener(this);
display.setcurrent(props);
}
valuesscreen()方法的逻辑是:首先创建一个容器对象form,然后调用textbox对象的getstr
ing()方法,获取文本框中的输入值,追加到容器对象中,最后将此form对象作为屏幕的当前显
示对象。gettextboxvalues.java的运行效果如下面两图所示:
(26)—–date对象
date对象是属于java.util包的,它的作用是返回当前的时间。请看下面的代码:
package fancy.test;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;
public class getdate extends midlet implements commandlistener
{
private display display;
private form props;
private date date;
private command exitcommand = new command("exit", command.exit, 1);
public getdate()
{
display = display.getdisplay(this);
}
public void startapp()
{
props = new form("hello world");
props.append("hello world!\n");
date=new date();
props.append("now time:"+date.gettime()+"\n");
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;
}
}
getdate.java程序的运行效果如下图所示:
————————————————————————————
网易广州社区java版
