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

来源:互联网 发布:淘宝漏洞免费买东西 编辑:程序博客网 时间:2024/05/16 13:03
本文来自:http://blog.csdn.net/hellogv/ ,转载必须注明出处!
首先先给出本例的效果图:


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

原创粉丝点击