本文来自: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 ?2008 Sun Microsystems, Inc. All rights reserved.
- * Use is subject to license terms.
- *
- */
- package com.sun.lwuit.uidemo;
- import com.sun.lwuit.Button;
- import com.sun.lwuit.Command;
- import com.sun.lwuit.Dialog;
- 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;
- import com.sun.lwuit.list.DefaultListModel;
- /**
- *本例演示如何使用List控件
- */
- public class ListDemo implements ActionListener {
- public Form form = new Form("ListDemo");
- private Command backCommand = new Command("Back", 1);
- private String[] str_list = {
- "aaaaaaaaaaaa",
- "bbbbbbbbbbbb",
- "ccccccccccccc",
- "ddddddddddddd"
- };
- ListDemo(){
- form.setLayout(new BorderLayout());
- form.addCommand(backCommand);
- form.setScrollable(true);
- //列表控件,尽管列表控件占用不少面积,但实际上跟普通的Componet一样
- DefaultListModel myListModel = new DefaultListModel(str_list);
- List list = new List(myListModel);
- list.getStyle().setBgTransparency(100);
- //按钮控件
- Button button = new Button("test");
- form.addComponent(BorderLayout.CENTER,list);
- form.addComponent(BorderLayout.NORTH,button);
- list.addActionListener(this);
- form.setCommandListener(this);
- }
- public void actionPerformed(ActionEvent arg0) {
- try{//处理列表事件
- String str=((List)(arg0.getSource())).getSelectedItem().toString();
- Dialog.show("ListDemo", str, "OK", null);
- }catch(Exception e)//处理COMMAND事件
- {
- Command command=arg0.getCommand();
- if(command==backCommand)
- UIDemoMIDlet.backToMainMenu();
- }
- }
- }