本文來自:http://blog.csdn.net/hellogv/ ,轉載必須註明出處!
首先先給出本例的:
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();
- }
- }
- }