本文来自:http://blog.csdn.net/hellogv/ ,转载必须注明出处!
首先先给出本例的效果图:
List在LWUIT中,可以有Button 与 BoxLayout-Y 取代,当然是在列项不多的时候。当列项多时,那就是LIST更省资源了!LWUIT的List比原List更强大,可以在LIST中实现一行存在多列的效果,并且背景还可以设置,不得不赞一下!
以下给出List最简单的使用代码:
- 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);
-
- 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=arg0.getCommand();
- if(command==backCommand)
- UIDemoMIDlet.backToMainMenu();
- }
- }
- }
发表于 @ 2008年09月17日 16:48:00|评论(loading...)|收藏