dave007

BlogJava 联系 聚合 管理
  0 Posts :: 3 Stories :: 16 Comments :: 0 Trackbacks
工作已经有两个星期了,工作任务是实现象腾讯的移动QQ一样的通讯工具。现在基本都搞定了,在美化阶段。
J2ME写界面真的好麻烦,搞界面也搞到我晕了!
下面贴一下我的部分界面代码出来!
这是一个用CANVAS来实现LIST的小功能。贴出来共响一下,代码写得不好。大家看看有没有利用价值,哈哈!
  1/**
  2 * author:weidong
  3 * date:2006-12-18
  4 * 重绘List(用低级界面实现List功能)
  5 * 功能简介:
  6 * 1、可以设置背景颜色    
  7 * 2、可以设置选择条的颜色  
  8 * 3、有滚动条  
  9 * 4、要适应屏幕的大小
 10 */

 11package org.weidong.ui;
 12
 13import java.util.Vector;
 14
 15import javax.microedition.lcdui.Canvas;
 16import javax.microedition.lcdui.Graphics;
 17
 18public class List extends Canvas{
 19    
 20    //存储LIST的项
 21    private Vector listVector=new Vector();
 22    //LIST里面的大小
 23    private int listVectorSize=0;
 24    //当前选择的序号
 25    private int currentSelect=0;
 26    //每行的高度
 27    private int fontHeight;
 28    //每行的间隔高度
 29    private int padding=2;
 30    //每页容纳的数量
 31    private int pageSize=0;
 32    //全部行相加的高度
 33    private int sumHeight=0;
 34    //是否已经配置选项
 35    private boolean isConfig=false;
 36    //是否已经画头部菜单
 37    private boolean isPaintHead=false;
 38    //是否已经画底部菜单
 39    private boolean isPaintBottom=false;
 40    //开始画LIST的起始位置
 41    private int topMenuHeight=0;;
 42    private int start=0;//
 43    int t=0;//当前选择条的位置
 44    int gunZuoBiao=getHeight()/12+2;//进度条坐标
 45    
 46    public List(){
 47        this.setFullScreenMode(true);
 48    }

 49
 50    protected void paint(Graphics g) {
 51        setConfig(g);
 52        paintBackGround(g);//画背景
 53        paintHead(g);//画头部
 54        paintSelctTool(g);
 55        paintList(g);//画主要部分list内容
 56        paintGun(g);//画滚动条
 57        paintBottom(g);//画底部
 58    }

 59    
 60
 61    /**
 62     * 画头部菜单
 63     * 菜单高度是    this.getHeight()/12
 64     * 底颜色:72,178,240
 65     * 可用图片来代替
 66     */

 67    public void paintHead(Graphics g){
 68        g.setColor(72,178,240);
 69        g.fillRect(00this.getWidth(),this.getHeight()/12);
 70        isPaintHead=true;
 71    }

 72    
 73    /**
 74     * 画底部菜单
 75     * 菜单高度是    this.getHeight()/12
 76     * 底颜色:72,178,240
 77     * 可用图片来代替
 78     */

 79    public void paintBottom(Graphics g){
 80        g.setColor(72,178,240);
 81        g.fillRect(0this.getHeight()*11/12this.getWidth(),this.getHeight()/12);
 82        isPaintBottom=true;
 83    }

 84    
 85    /**
 86     * 画菜单中心 LIST的选项
 87     * @param g: 为Graphics对象
 88     */

 89    public void paintList(Graphics g){
 90        int j=0;//当前页的序号
 91        g.setColor(Platform.BLACK);
 92        for(int i=start;i<listVectorSize;i++){
 93            if(j<pageSize){
 94                g.drawString((String)listVector.elementAt(i), 4,topMenuHeight+j*fontHeight+j*padding, Graphics.TOP|Graphics.LEFT);
 95                j++;
 96            }

 97        }

 98    }

 99    
100    /**
101     * 画背景 Platform.WHITE为设置的背景颜色
102     * @param g
103     */

104    public void paintBackGround(Graphics g){
105        int color=Platform.WHITE;
106        g.setColor(color);
107        g.fillRect(00this.getWidth(),this.getHeight());
108    }

109    
110    /**
111     * 刷新背景
112     * @param g
113     */

114    public void refreshBackGroup(Graphics g){
115        g.setColor(255255255);
116        g.fillRect(00this.getWidth(),this.getHeight());
117    }

118    
119    /**
120     * 按键事件响应
121     */

122    public void keyPressed(int keyCode){
123        
124        //选择响应事件
125        switch(keyCode){
126            //向下
127            case Platform.KEY_DOWN:{
128                //如果currentSelect少于LIST里面的元素时候,则可以按向下操作
129                if(currentSelect<(listVectorSize-1)){
130                    //如果currentSelect大于一页的容量并且选择条选到当页的最后一条时候,则选择条维持在最后一条的位置,并使START向下移一位
131                    if(currentSelect>=(pageSize-1)&&t==(pageSize-1)){
132                        t=pageSize-1;
133                        start=currentSelect-pageSize+2;
134                    }

135                    //当选择条不在最后一条的状态下,则可以不断对选择条移下
136                    else if(t!=(pageSize-1)){
137                        t++;
138                    }

139                    //滚动坐标
140                    gunZuoBiao=(int)(gunZuoBiao+(getHeight()-2*(getHeight()/12+2))/listVectorSize);
141                    currentSelect++;
142                    repaint();
143                }

144            }
;break;
145            
146            //向上
147            case Platform.KEY_UP:{
148                if(currentSelect>0){
149                    if(t<=(pageSize-1)&&t>0){
150                        --t;
151                        --currentSelect;
152                        gunZuoBiao=(int)(gunZuoBiao-(getHeight()-2*(getHeight()/12+2))/listVectorSize);
153                        repaint();
154                    }
else if(t==0){
155                        --currentSelect;
156                        start=currentSelect;
157                        //滚动坐标
158                        gunZuoBiao=(int)(gunZuoBiao-(getHeight()-2*(getHeight()/12+2))/listVectorSize);
159                        repaint();
160                    }

161                }

162            }
;break;
163            
164            //向左
165            case Platform.KEY_LEFT:;break;
166            
167            //向右
168            case Platform.KEY_RIGHT:;break;
169            
170            //左软键
171            case Platform.KEY_SOFT_LEFT:;break;
172            
173            //右软键
174            case Platform.KEY_SOFT_RIGHT:;break;
175        }

176    }

177    
178    
179    public void setData(String stringItem){
180        this.addStringItem(stringItem);
181    }

182    
183    public void setBackGroundColor(int color){
184        
185    }

186    /**
187     * 基本配置
188     * @param g
189     */

190    public void setConfig(Graphics g){
191        fontHeight=g.getFont().getHeight()+2;//确定每行的高度
192        pageSize=((getHeight()*10/12)/(fontHeight+padding));//确定每页可以容纳的行数
193        sumHeight=fontHeight*listVectorSize;//确定全部行相加的总高度
194        topMenuHeight=this.getHeight()/12+2;//上面菜单高度
195        isConfig=true;//设置已经配置
196    }

197    
198    /**
199     * @para stringItem:将要插入list的字符串(加入List的字符串)
200     */

201    public void addStringItem(String stringItem){
202        listVector.addElement(stringItem);
203        listVectorSize=listVector.size();
204    }

205    
206    /**
207     * @param listIndex:要取的list里面的某个位置listIndex
208     * @return 返回list中listIndex位置的字符串
209     */

210    public String getStringItem(int listIndex){
211        return (String)listVector.elementAt(listIndex);
212    }

213    
214    /**
215     * @return 返回list的大小
216     */

217    public int getListSize(){
218        return listVector.size();
219    }

220    
221    
222    /**
223     * 绘制选择条
224     * topMenuHeight+t*fontHeight+t*padding :画好友的选择条的开始坐标
225     * topMenuHeight最上面的栏目菜单,包括2px的间隔。
226     * t*fontHeight是一个好友的高度
227     * t*padding是好友之间的间隔
228     * t----标记第几行
229     */

230    public void paintSelctTool(Graphics g){
231        g.setColor(205,236,254);
232        g.fillRect(0, topMenuHeight+t*fontHeight+t*padding,this.getWidth(),fontHeight);
233        g.setColor(72,178,240);
234        g.drawLine(0, topMenuHeight+t*fontHeight+t*padding, this.getWidth(), topMenuHeight+t*fontHeight+t*padding);
235        g.drawLine(0, topMenuHeight+(t+1)*fontHeight+t*padding, this.getWidth(),topMenuHeight+(t+1)*fontHeight+t*padding);
236    }

237    
238    /**
239     * 画滚动条
240     * @param g
241     */

242    public void paintGun(Graphics g){
243        g.setColor(213,213,213);
244        g.fillRect(getWidth()-4,getHeight()/12+2 ,1,(getHeight()-2*(getHeight()/12+2)));
245        g.setColor(72,178,240);
246        g.fillRect(getWidth()-4,gunZuoBiao ,2,12);
247    }

248    
249    
250}

251

实现效果图~~
{2A1F7297-B1D0-4D02-A195-4EEF5A6109C0}.BMP{D8E89D5C-1967-4B26-B76A-15CC8F2390A0}.BMP
posted on 2006-12-19 15:34 D.a.v.e 阅读(2079) 评论(15)  编辑  收藏 所属分类: j2me

Feedback

# re: 用CANVAS来实现LIST的功能 2006-12-25 22:41 小T
师傅,比心机啦,你掂噶:)  回复  更多评论
  

# re: 用CANVAS来实现LIST的功能 2007-01-09 23:39 蚊蚊
师兄```好犀利啊!!加油```我知道你好犀利嘎,就算遇到咩困难,都希望你可以坚持自己噶理想啦````放心拉,我会派我D蚊仔军团支持你噶拉``  回复  更多评论
  

# re: 用CANVAS来实现LIST的功能 2007-04-07 09:10 xhjd
非常不错地说  回复  更多评论
  

# re: 用CANVAS来实现LIST的功能 2007-05-10 09:18 ddkl
师兄好强啊。。。。  回复  更多评论
  

# re: 用CANVAS来实现LIST的功能 2008-04-25 13:40 yylong
有没有完整的源代码啊?可以发给我一份吗,谢谢了
急用啊,我的邮箱是yyl_619@163.com  回复  更多评论
  

# re: 用CANVAS来实现LIST的功能 2008-07-22 09:44 陈智旭
你好,能发我一份完整的代码学习下吗?我把上面的东西考过去的时候显示Platform这个类没有啊,怎么回事?我邮箱black860@163.com,谢谢。  回复  更多评论
  

# re: 用CANVAS来实现LIST的功能 2009-01-23 18:42 chang
呵呵,不错  回复  更多评论
  

# re: 用CANVAS来实现LIST的功能 2009-02-20 13:31 j2me菜鸟
博主,能否把完整的代码发份给我学习下呢?小弟感激不尽.我的邮箱mfdipq@163.com!谢谢!  回复  更多评论
  

# re: 用CANVAS来实现LIST的功能 2009-06-02 15:10 不要昵称
真牛,能否,把完整的代码发给我学习学习。我的邮箱是wliang0746@yahoo.cn  回复  更多评论
  

# re: 用CANVAS来实现LIST的功能 2009-06-09 09:15 hit1063710428
正在学习Canvas来绘制自己的页面,正好遇到了一个页面装不下,需要滚动的问题。能把你的代码发给我下吗?
邮箱是hit1063710428@gmail.com
谢谢啦  回复  更多评论
  

# re: 用CANVAS来实现LIST的功能 2009-07-20 17:21 j2me大菜鸟
正在学习Canvas来绘制自己的页面,可以给个完整代码吗。界面我已经调出来了。邮箱490204287@qq.com  回复  更多评论
  

# re: 用CANVAS来实现LIST的功能 2009-08-03 09:41 sxf
太好了,有没有完整代码发我下学习学习sxf_snoopy@sohu.com
不胜感激  回复  更多评论
  

# re: 用CANVAS来实现LIST的功能 2009-08-13 12:02 pochonlee
public class Platform {
public static final int KEY_UP = -1;
public static final int KEY_DOWN = -2;
public static final int KEY_LEFT = -3;
public static final int KEY_SOFT_LEFT = -6;
public static final int KEY_SOFT_RIGHT = -7;
public static final int KEY_RIGHT = -4;
public static int BLACK = 0x00000000;
public static int WHITE = 0x00FFFFFF;
}  回复  更多评论
  

# re: 用CANVAS来实现LIST的功能 2009-12-21 18:07 光耀
希望能在qq上请教你
我的 qq 1225428685  回复  更多评论
  

# re: 用CANVAS来实现LIST的功能 2009-12-29 18:57 EnjoyJ2ME
不知道小图标是如何添加的啊?可否指点一二!  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: