本文来自:http://blog.youkuaiyun.com/hellogv/ ,转载必须注明出处!
首先先给出本例的效果图:

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

List在LWUIT中,可以有Button 与 BoxLayout-Y 取代,当然是在列项不多的时候。当列项多时,那就是LIST更省资源了!LWUIT的List比原List更强大,可以在LIST中实现一行存在多列的效果,并且背景还可以设置,不得不赞一下!
以下给出List最简单的使用代码:
- /*
- *Copyright?2008SunMicrosystems,Inc.Allrightsreserved.
- *Useissubjecttolicenseterms.
- *
- */
- packagecom.sun.lwuit.uidemo;
- importcom.sun.lwuit.Button;
- importcom.sun.lwuit.Command;
- importcom.sun.lwuit.Dialog;
- importcom.sun.lwuit.Form;
- importcom.sun.lwuit.List;
- importcom.sun.lwuit.events.ActionEvent;
- importcom.sun.lwuit.events.ActionListener;
- importcom.sun.lwuit.layouts.BorderLayout;
- importcom.sun.lwuit.list.DefaultListModel;
- /**
- *本例演示如何使用List控件
- */
- publicclassListDemoimplementsActionListener{
- publicFormform=newForm("ListDemo");
- privateCommandbackCommand=newCommand("Back",1);
- privateString[]str_list={
- "aaaaaaaaaaaa",
- "bbbbbbbbbbbb",
- "ccccccccccccc",
- "ddddddddddddd"
- };
- ListDemo(){
- form.setLayout(newBorderLayout());
- form.addCommand(backCommand);
- form.setScrollable(true);
- //列表控件,尽管列表控件占用不少面积,但实际上跟普通的Componet一样
- DefaultListModelmyListModel=newDefaultListModel(str_list);
- Listlist=newList(myListModel);
- list.getStyle().setBgTransparency(100);
- //按钮控件
- Buttonbutton=newButton("test");
- form.addComponent(BorderLayout.CENTER,list);
- form.addComponent(BorderLayout.NORTH,button);
- list.addActionListener(this);
- form.setCommandListener(this);
- }
- publicvoidactionPerformed(ActionEventarg0){
- try{//处理列表事件
- Stringstr=((List)(arg0.getSource())).getSelectedItem().toString();
- Dialog.show("ListDemo",str,"OK",null);
- }catch(Exceptione)//处理COMMAND事件
- {
- Commandcommand=arg0.getCommand();
- if(command==backCommand)
- UIDemoMIDlet.backToMainMenu();
- }
- }
- }