WEB开发网
开发学院软件开发Java J2ME GUI实战之八 ----------LWUIT的Li... 阅读

J2ME GUI实战之八 ----------LWUIT的List控件

 2009-09-12 00:00:00 来源:WEB开发网   
核心提示:首先先给出本例的效果图:List在LWUIT中,可以有Button 与 BoxLayout-Y 取代,J2ME GUI实战之八 ----------LWUIT的List控件,当然是在列项不多的时候,当列项多时,并且背景还可以设置,不得不赞一下!以下给出List最简单的使用代码: 1. /* 2. * Copyright

首先先给出本例的效果图:

J2ME GUI实战之八 ----------LWUIT的List控件

List在LWUIT中,可以有Button 与 BoxLayout-Y 取代,当然是在列项不多的时候。当列项多时,那就是LIST更省资源了!LWUIT的List比原List更强大,可以在LIST中实现一行存在多列的效果,并且背景还可以设置,不得不赞一下!

以下给出List最简单的使用代码:

   1. /*
   2.  * Copyright ?2008 Sun Microsystems, Inc. All rights reserved.
   3.  * Use is subject to license terms.
   4.  *
   5.  */
   6. package com.sun.lwuit.uidemo;
   7.
   8. import com.sun.lwuit.Button;
   9. import com.sun.lwuit.Command;
  10. import com.sun.lwuit.Dialog;
  11. import com.sun.lwuit.Form;
  12. import com.sun.lwuit.List;
  13. import com.sun.lwuit.events.ActionEvent;
  14. import com.sun.lwuit.events.ActionListener;
  15. import com.sun.lwuit.layouts.BorderLayout;
  16. import com.sun.lwuit.list.DefaultListModel;
  17.
  18. /**
  19.  *本例演示如何使用List控件
  20.  */
  21. public class ListDemo implements ActionListener  {
  22.     public Form form = new Form("ListDemo");
  23.     private  Command backCommand = new Command("Back", 1);
  24.
  25.     private String[] str_list = {
  26.     "aaaaaaaaaaaa",
  27.     "bbbbbbbbbbbb",
  28.     "ccccccccccccc",
  29.     "ddddddddddddd"
  30.     };
  31.
  32.     ListDemo(){
  33.         form.setLayout(new BorderLayout());
  34.         form.addCommand(backCommand);
  35.         form.setScrollable(true);
  36.
  37.         //列表控件,尽管列表控件占用不少面积,但实际上跟普通的Componet一样
  38.         DefaultListModel myListModel = new DefaultListModel(str_list);
  39.         List list = new List(myListModel);
  40.         list.getStyle().setBgTransparency(100);
  41.
  42.         //按钮控件
  43.         Button button = new Button("test");
  44.
  45.         form.addComponent(BorderLayout.CENTER,list);
  46.         form.addComponent(BorderLayout.NORTH,button);
  47.
  48.         list.addActionListener(this);
  49.         form.setCommandListener(this);
  50.     }
  51.
  52.     public void actionPerformed(ActionEvent arg0) {
  53.           
  54.         try{//处理列表事件
  55.             String str=((List)(arg0.getSource())).getSelectedItem().toString();
  56.             Dialog.show("ListDemo", str, "OK", null);
  57.         }catch(Exception e)//处理COMMAND事件
  58.         {
  59.             Command command=arg0.getCommand();
  60.             if(command==backCommand)
  61.                 UIDemoMIDlet.backToMainMenu();
  62.         }
  63.     }
  64. }
  65.

Tags:JME GUI 实战

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接