站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2ME 专题J2ME学习札记 22-26

J2ME学习札记 22-26

摘要: J2ME学习札记 22-26
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版




↑返回目录
前一篇: J2ME学习札记 27-28
后一篇: J2ME学习札记 17-21