
工作已经有两个星期了,工作任务是实现象腾讯的移动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
*/
11
package org.weidong.ui;
12
13
import java.util.Vector;
14
15
import javax.microedition.lcdui.Canvas;
16
import javax.microedition.lcdui.Graphics;
17
18
public 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(0, 0, this.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(0, this.getHeight()*11/12, this.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(0, 0, this.getWidth(),this.getHeight());
108
}
109
110
/** *//**
111
* 刷新背景
112
* @param g
113
*/
114
public void refreshBackGroup(Graphics g)
{
115
g.setColor(255, 255, 255);
116
g.fillRect(0, 0, this.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
实现效果图~~
