J2ME GUI实战之八 ----------LWUIT的List控件

本文介绍 LWUIT 中 List 控件的基本用法及优势,通过实例展示如何创建 List 控件并设置其属性,包括如何响应用户交互事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


List在LWUIT中,可以有Button 与 BoxLayout-Y 取代,当然是在列项不多的时候。当列项多时,那就是LIST更省资源了!LWUIT的List比原List更强大,可以在LIST中实现一行存在多列的效果,并且背景还可以设置,不得不赞一下!
以下给出List最简单的使用代码:
  1. /*
  2. *Copyright?2008SunMicrosystems,Inc.Allrightsreserved.
  3. *Useissubjecttolicenseterms.
  4. *
  5. */
  6. packagecom.sun.lwuit.uidemo;
  7. importcom.sun.lwuit.Button;
  8. importcom.sun.lwuit.Command;
  9. importcom.sun.lwuit.Dialog;
  10. importcom.sun.lwuit.Form;
  11. importcom.sun.lwuit.List;
  12. importcom.sun.lwuit.events.ActionEvent;
  13. importcom.sun.lwuit.events.ActionListener;
  14. importcom.sun.lwuit.layouts.BorderLayout;
  15. importcom.sun.lwuit.list.DefaultListModel;
  16. /**
  17. *本例演示如何使用List控件
  18. */
  19. publicclassListDemoimplementsActionListener{
  20. publicFormform=newForm("ListDemo");
  21. privateCommandbackCommand=newCommand("Back",1);
  22. privateString[]str_list={
  23. "aaaaaaaaaaaa",
  24. "bbbbbbbbbbbb",
  25. "ccccccccccccc",
  26. "ddddddddddddd"
  27. };
  28. ListDemo(){
  29. form.setLayout(newBorderLayout());
  30. form.addCommand(backCommand);
  31. form.setScrollable(true);
  32. //列表控件,尽管列表控件占用不少面积,但实际上跟普通的Componet一样
  33. DefaultListModelmyListModel=newDefaultListModel(str_list);
  34. Listlist=newList(myListModel);
  35. list.getStyle().setBgTransparency(100);
  36. //按钮控件
  37. Buttonbutton=newButton("test");
  38. form.addComponent(BorderLayout.CENTER,list);
  39. form.addComponent(BorderLayout.NORTH,button);
  40. list.addActionListener(this);
  41. form.setCommandListener(this);
  42. }
  43. publicvoidactionPerformed(ActionEventarg0){
  44. try{//处理列表事件
  45. Stringstr=((List)(arg0.getSource())).getSelectedItem().toString();
  46. Dialog.show("ListDemo",str,"OK",null);
  47. }catch(Exceptione)//处理COMMAND事件
  48. {
  49. Commandcommand=arg0.getCommand();
  50. if(command==backCommand)
  51. UIDemoMIDlet.backToMainMenu();
  52. }
  53. }
  54. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值