ticker在screen类中被定义为一个域(Field),它可以被连接到任何screen类的子集中。在下面的例子中,我们将展示如何将ticker连接到列表(List)和alert对象中。
注意:以下例子基于MIDP和CLDC 1.0.3
源代码: /*--------------------------------------------------
* TickerExample.java
*-------------------------------------------------*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class TickerExample extends MIDlet implements CommandListener
{
private Display display; // 对显示对象的引用Reference to Display object
private List lsProducts; // 主产品列表
private Alert alHelp; // 显现文字和图像的警告板
private Ticker tkSale; // 对现在销售的提示
private Command cmExit; // 退出命令
public TickerExample()
{
display = Display.getDisplay(this);
cmExit = new Command("Exit", Command.SCREEN, 1);
tkSale = new Ticker("Current Sale: Bob's Pretty Good Laptop only $529.00");
lsProducts = new List("Products", Choice.IMPLICIT);
lsProducts.append("Laptop", null);
lsProducts.append("Printer", null);
lsProducts.append("Help", null);
lsProducts.addCommand(cmExit);
lsProducts.setCommandListener(this);
lsProducts.setTicker(tkSale);
}
public void startApp()
{
display.setCurrent(lsProducts);
}
public void pauseApp()
{ }
public void destroyApp(boolean unconditional)
{ }
public void showAlert()
{
try
{
// Create an image
Image im = Image.createImage("/help.png");
// Create Alert, add text and image, no sound
alHelp = new Alert("Help Information",
"Bob's Pretty Good Products...so much to choose from!",
im, null);
alHelp.setTimeout(Alert.FOREVER);
alHelp.setTicker(tkSale);
}
catch(Exception e)
{
System.out.println("Unable to read png image.");
}
// Display the Alert. Once dismissed, return to product list
display.setCurrent(alHelp, lsProducts);
}
public void commandAction(Command c, Displayable s)
{
if (c == List.SELECT_COMMAND)
{
switch (lsProducts.getSelectedIndex())
{
case 0:
System.out.println("Laptop selected");
break;
case 1:
System.out.println("Printer selected");
break;
case 2:
showAlert();
break;
}
}
else if (c == cmExit)
{
destroyApp(true);
notifyDestroyed();
}
}
}