六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 76|回复: 0

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

[复制链接]

升级  49%

233

主题

233

主题

233

主题

进士

Rank: 4

积分
745
 楼主| 发表于 2013-2-5 09:04:55 | 显示全部楼层 |阅读模式
本文来自:http://blog.csdn.net/hellogv/ ,转载必须注明出处!
首先先给出本例的效果图:


List在LWUIT中,可以有Button 与 BoxLayout-Y 取代,当然是在列项不多的时候。当列项多时,那就是LIST更省资源了!LWUIT的List比原List更强大,可以在LIST中实现一行存在多列的效果,并且背景还可以设置,不得不赞一下!
以下给出List最简单的使用代码:
<div class="highlighter">

  • /*
  • *Copyright?2008SunMicrosystems,Inc.Allrightsreserved.
  • *Useissubjecttolicenseterms.
  • *
  • */
  • packagecom.sun.lwuit.uidemo;

  • importcom.sun.lwuit.Button;
  • importcom.sun.lwuit.Command;
  • importcom.sun.lwuit.Dialog;
  • importcom.sun.lwuit.Form;
  • importcom.sun.lwuit.List;
  • importcom.sun.lwuit.events.ActionEvent;
  • importcom.sun.lwuit.events.ActionListener;
  • importcom.sun.lwuit.layouts.BorderLayout;
  • importcom.sun.lwuit.list.DefaultListModel;

  • /**
  • *本例演示如何使用List控件
  • */
  • publicclassListDemoimplementsActionListener{
  • publicFormform=newForm("ListDemo");
  • privateCommandbackCommand=newCommand("Back",1);

  • privateString[]str_list={
  • "aaaaaaaaaaaa",
  • "bbbbbbbbbbbb",
  • "ccccccccccccc",
  • "ddddddddddddd"
  • };

  • ListDemo(){
  • form.setLayout(newBorderLayout());
  • form.addCommand(backCommand);
  • form.setScrollable(true);

  • //列表控件,尽管列表控件占用不少面积,但实际上跟普通的Componet一样
  • DefaultListModelmyListModel=newDefaultListModel(str_list);
  • Listlist=newList(myListModel);
  • list.getStyle().setBgTransparency(100);

  • //按钮控件
  • Buttonbutton=newButton("test");

  • form.addComponent(BorderLayout.CENTER,list);
  • form.addComponent(BorderLayout.NORTH,button);

  • list.addActionListener(this);
  • form.setCommandListener(this);
  • }

  • publicvoidactionPerformed(ActionEventarg0){

  • try{//处理列表事件
  • Stringstr=((List)(arg0.getSource())).getSelectedItem().toString();
  • Dialog.show("ListDemo",str,"OK",null);
  • }catch(Exceptione)//处理COMMAND事件
  • {
  • Commandcommand=arg0.getCommand();
  • if(command==backCommand)
  • UIDemoMIDlet.backToMainMenu();
  • }
  • }
  • }

您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表