`

lwuit list

阅读更多
1.下面两个java文件是一个实例,直接放在工程下就可以直接看到效果。
  效果图见下面的上传文件
Cls_CellList. java主要使用了ListCellRenderer这个东西,源代码如下:
package com.sun.lwuit.uidemo;  
import com.sun.lwuit.Component;  
import com.sun.lwuit.Container;  
import com.sun.lwuit.Label;  
import com.sun.lwuit.List;  
import com.sun.lwuit.events.ActionListener;  
import com.sun.lwuit.layouts.BorderLayout;  
import com.sun.lwuit.layouts.BoxLayout;  
import com.sun.lwuit.list.ListCellRenderer;  
import com.sun.lwuit.plaf.Border;  
public class Cls_CellList {  
    static public List createList(CellList[] celllist,int stringWidth,ActionListener lAListner) {  
        List list = new List(celllist);  
        list.getStyle().setBgTransparency(0);  
        //开始显示List  
        list.setListCellRenderer(new CellRenderer(celllist[0].getColumm().length,stringWidth));  
        //添加消息处理  
        list.addActionListener(lAListner);  
        return list;  
    }  
static public class CellRenderer extends Container implements ListCellRenderer {  
        //初始化显示Columm每个字段的Label  
        private Label[] lbColumm ;  
        private Label focus = new Label("");  
        public CellRenderer(int size,final int stringWidth) {  
            lbColumm = new Label[size];  
            setLayout(new BorderLayout());  
            Container cnt = new Container(new BoxLayout(BoxLayout.X_AXIS));  
            for(int i=0;i<lbColumm.length;i++)//定义显示Columm每个字段的Label  
            {  
                lbColumm[i]=new Label(){  
                    //很多人都问怎么setWidth平时没什么用,这是因为setWidth必须在重载的时候使用,LWUIT奇怪的地方之一!  
                    public void setWidth(int arg0) {  
                        super.setWidth(stringWidth);  
                    }  
                };  
                lbColumm[i].getStyle().setFgColor(getStyle().getFgSelectionColor());//设置为最亮的颜色  
                lbColumm[i].getStyle().setBgTransparency(0);//设置背景为透明,不然会遮挡focus影响外观  
                cnt.addComponent(lbColumm[i]);  
            }  
              
            addComponent(BorderLayout.CENTER, cnt);  
            focus.getStyle().setBgTransparency(255);  
            focus.getStyle().setBorder(Border.createRoundBorder(10, 10));  
        }  
        public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected) {  
            CellList cellList = (CellList) value;  
            for(int i=0;i<lbColumm.length;i++)  
            {  
                lbColumm[i].setText(cellList.getColumm()[i]);  
            }  
            return this;  
        }  
        public Component getListFocusComponent(List list) {  
            //Columm栏不具备选中事件  
            if(list.getSelectedIndex()>0)  
                return focus;  
            return null;  
        }  
    }  
    static public class CellList {  
        //保存该列的所有字段  
        private String[] lstColumm;  
        public CellList(String[] columm) {  
           //复制数组  
           lstColumm=new String[columm.length];  
           System.arraycopy(columm, 0, lstColumm, 0, lstColumm.length);  
        }  
        //返回该列  
        public String[] getColumm(){  
           return lstColumm;  
        }  
    }  


ListDemo.java源代码如下:





/* 
* Copyright ?2008 Sun Microsystems, Inc. All rights reserved. 
* Use is subject to license terms. 

*/ 
package com.sun.lwuit.uidemo;  
import com.sun.lwuit.Command;  
import com.sun.lwuit.Font;  
import com.sun.lwuit.Form;  
import com.sun.lwuit.List;  
import com.sun.lwuit.events.ActionEvent;  
import com.sun.lwuit.events.ActionListener;  
import com.sun.lwuit.layouts.BorderLayout;  
public class ListDemo implements ActionListener  {  
    public Form form = new Form("ListDemo");  
    private  Command backCommand = new Command("Back", 1);  
    private String[][] myList = {  
        {"  标题1  ","  标题2  ","  标题3  "," 标题4 "},//表格每个字段的显示长度由标题栏的最长一项决定  
        {"aaaaa","bbbbb","ccccc","ddddd"},  
        {"11111","222222","33333","444444"},  
        {"aaaaa","bbbbb","ccccc","ddddd"},  
        {"11111","222222","33333","444444"},  
        {"aaaaa","bbbbb","ccccc","ddddd"},  
        {"11111","222222","33333","444444"},  
        {"aaaaa","bbbbb","ccccc","ddddd"},  
        {"11111","222222","33333","444444"}  
    };  
    ListDemo(){  
        form.setLayout(new BorderLayout());  
        form.addCommand(backCommand);  
        form.setScrollable(true);  
        //定义列表的列数  
        Cls_CellList.CellList[] cellArray = new Cls_CellList.CellList[myList.length];  
        for (int i = 0; i < cellArray.length; i++) {  
            cellArray[i] = new Cls_CellList.CellList(myList[i]);  
        }  
          
        //取得Columm的标题栏中长度最大的一项  
        int stringWidth=0;  
        for(int i=0;i<myList[0].length;i++)  
        {  
            int size=Font.getDefaultFont().stringWidth(myList[0][i])+8;//给最长的字段留点空余  
            if(stringWidth<size)  
                stringWidth=size;  
        }  
        //构造list并且显示  
        form.addComponent(BorderLayout.CENTER,Cls_CellList.createList(cellArray,stringWidth,new ListActionListener()));  
        form.setCommandListener(this);  
    }  
    public void actionPerformed(ActionEvent arg0) {    
        Command command=arg0.getCommand();  
        if(command==backCommand)  
            UIDemoMIDlet.backToMainMenu();  
    }  
    //按下列表触发的事件  
    private class ListActionListener implements ActionListener {  
        public void actionPerformed(ActionEvent evt) {  
            int selectIndex=((List)evt.getSource()).getSelectedIndex();  
            String str="";  
            for(int i=0;i<myList[selectIndex].length;i++)  
                str=str+myList[selectIndex][i]+"_";  
            form.setTitle(str);  
        }  
    }  



本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/hellogv/archive/2009/11/04/4767263.aspx


















本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/hellogv/archive/2009/11/04/4767263.aspx
  • 大小: 1.8 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics