J2ME GUI實戰之八 ———-LWUIT的List控制項

來源:互聯網
上載者:User
本文來自:http://blog.csdn.net/hellogv/ ,轉載必須註明出處!
首先先給出本例的:

    List在LWUIT中,可以有Button 與 BoxLayout-Y 取代,當然是在列項不多的時候。當列項多時,那就是LIST更省資源了!LWUIT的List比原List更強大,可以在LIST中實現一行存在多列的效果,並且背景還可以設定,不得不贊一下!
    以下給出List最簡單的使用代碼:

  1. /*
  2.  * Copyright ?2008 Sun Microsystems, Inc. All rights reserved.
  3.  * Use is subject to license terms.
  4.  *
  5.  */
  6. package com.sun.lwuit.uidemo;
  7. import com.sun.lwuit.Button;
  8. import com.sun.lwuit.Command;
  9. import com.sun.lwuit.Dialog;
  10. import com.sun.lwuit.Form;
  11. import com.sun.lwuit.List;
  12. import com.sun.lwuit.events.ActionEvent;
  13. import com.sun.lwuit.events.ActionListener;
  14. import com.sun.lwuit.layouts.BorderLayout;
  15. import com.sun.lwuit.list.DefaultListModel;
  16. /**
  17.  *本例示範如何使用List控制項
  18.  */
  19. public class ListDemo implements ActionListener  {
  20.     public Form form = new Form("ListDemo");
  21.     private  Command backCommand = new Command("Back", 1);
  22.     private String[] str_list = {
  23.     "aaaaaaaaaaaa",
  24.     "bbbbbbbbbbbb",
  25.     "ccccccccccccc",
  26.     "ddddddddddddd"
  27.     };
  28.     ListDemo(){
  29.         form.setLayout(new BorderLayout());
  30.         form.addCommand(backCommand);
  31.         form.setScrollable(true);
  32.         //清單控制項,儘管清單控制項佔用不少面積,但實際上跟普通的Componet一樣
  33.         DefaultListModel myListModel = new DefaultListModel(str_list);
  34.         List list = new List(myListModel);
  35.         list.getStyle().setBgTransparency(100);
  36.         //按鈕控制項
  37.         Button button = new Button("test");
  38.         form.addComponent(BorderLayout.CENTER,list);
  39.         form.addComponent(BorderLayout.NORTH,button);
  40.         list.addActionListener(this);
  41.         form.setCommandListener(this);
  42.     }
  43.     public void actionPerformed(ActionEvent arg0) {
  44.            
  45.         try{//處理列表事件
  46.             String str=((List)(arg0.getSource())).getSelectedItem().toString();
  47.             Dialog.show("ListDemo", str, "OK", null);
  48.         }catch(Exception e)//處理COMMAND事件
  49.         {
  50.             Command command=arg0.getCommand();
  51.             if(command==backCommand)
  52.                 UIDemoMIDlet.backToMainMenu();
  53.         }
  54.     }
  55. }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.