到 Google 资讯主页   
EasyJF首页   资料   源码   软件    论坛   网站    
   使用帮助    
    该信息为本站MyRSS系统缓存内容,部分图片及附件有可能无法正常使用.easyjf.comwww.matrix.org.cn无关,不对该信息负责.通过http://www.matrix.org.cn//resource/article/0/544.html访问该信息的原始内容.
页面功能  【加入收藏】 【推荐给朋友】 【字体:  】 【关闭】   
在组件(components)之间共享Ticker
作者:chris 来源:www.matrix.org.cn  发布时间:2006-02-22 17:48:46.89

    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();
    }
  }
}



模拟器显示:
image

源代码下载:

下载
点击参与本文章相关讨论

关于翻译作者:
bruceyuki,JAVA C#技术爱好者,现就读于新西兰奥克兰大学,正参与大学的一个AI项目,可以点击http://www.matrix.org.cn/user_view.asp?username=bruceyuki查看他的个人信息

 
相关文章
 
页面功能  【加入收藏】 【推荐给朋友】 【字体:  】 【关闭】   


EasyJF.com 2006 隐私政策 使用EasyJF前必读