- 论坛徽章:
- 0
|
最近在做一个手机系统,发现需要很多的list,如:电话薄,记事本,文件管理……
虽然J2ME带有List,但是界面不是很好看,所以我就抽了个时间,写了这个CList(CanvasList)类,以便于总是做一些重复的工
作,当然了,功能肯定不能和JAVA自带的List强大了,不过还是实现了添加字符串,图片功能,还有设置背景图片,颜色,获
取当前索引,因为写程序近一年,所以还有好多地方不够专业,也没有打稿子,大家觉得还可以就拿去用吧!
以下是源代码:
import java.util.Vector;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
public class CList extends Canvas {
/**********
* 定义使用成员
* @param
* @author 李鸿
**********/
private Vector strList = new Vector(); //字符向量
private Vector imgList = new Vector(); //图片向量
private String title = null; //标题
private int strListSize = 0; //字符向量大小
private int imgListSize = 0; //图片向量大小
private int BackGroundColor = 0xefefef; //背景颜色
private Image BackGroudImage = null; //背景图片
Font font = Font.getDefaultFont(); //背景字体
private int imgWidth = 0; //图片的高
private int imgHeight = 0; //图片的高
private int width = 0; //画布的宽
private int height = 0; //画布的高
private int location = 1; //滚动条的当前位置
private int selectIndex = 0; //选择的索引
private int selectY = 0; //选择框的纵坐标
private int addValue = 0; //超过显示最大值自动增长值
/**********
* 构造函数
* @param
* @author 李鸿
**********/
public CList(String title){
super();
this.setFullScreenMode(true);
this.title = title;
this.initList();
}
/**********
* 设置标题
* @param
* @author 李鸿
**********/
public void setTitle(String title){
this.title = title;
}
/**********
* 设置背景颜色
* @param
* @author 李鸿
**********/
public void setBackGroundColor(int BackGroundColor){
this.BackGroundColor = BackGroundColor;
}
/**********
* 设置背景图片
* @param
* @author 李鸿
**********/
public Image setBackGroudImage(Image image){
this.BackGroudImage = image;
return BackGroudImage;
}
/**********
* 设置字体
* @param
* @author 李鸿
**********/
public void setFont(Font font){
this.font = font;
}
/**********
* 获取字符串列表的的大小
* @param
* @author 李鸿
**********/
public int getStringListSize(){
return strList.size();
}
/**********
* 获取图片列表的大小
* @param
* @author 李鸿
**********/
public int getImageListSize(){
return imgList.size();
}
/**********
* 获取当前索引
* @param
* @author 李鸿
**********/
public int getSelectIndex(){
return selectIndex;
}
/**********
* 获取当前选择的字符串
* @param
* @author 李鸿
**********/
public String getStringItem(int index){
return (String)strList.elementAt(index);
}
/**********
* 获取当前选择的图片
* @param
* @author 李鸿
**********/
public Image getImageItem(int index){
return (Image)imgList.elementAt(index);
}
/**********
* 添加字符串和图片到Vector中
* @param
* @author 李鸿
**********/
public void append(String string,Image image){
if(string == null){
}else{
strList.addElement(string);
strListSize = strList.size();
}
if(image == null){
}else{
imgWidth = image.getWidth();
imgHeight = image.getHeight();
imgList.addElement(image);
imgListSize = imgList.size();
}
}
/**********
* 清除全部数据方法
*
* @param
* @author 李鸿
**********/
public void deleteAll() {
strList.removeAllElements();
imgList.removeAllElements();
}
/**********
* 绘制背景颜色
* @param
* @author 草榴社区李鸿
**********/
public void DrawBackGround(Graphics g){
g.setColor(BackGroundColor);
g.fillRect(0, 0, width,height);
if(BackGroudImage != null){
g.drawImage(BackGroudImage, 0, 0, 0);
}
}
/**********
* 绘制头部
* @param
* @author 李鸿
**********/
public void DrawHead(Graphics g){
g.setColor(126,126,126);
g.drawRect(0, 0, width, height/16);
g.setColor(72,178,240);
g.fillRect(1, 1, width-1,height/16-2);
g.setColor(201,255,255);
g.drawLine(1, height/16-1, width-1, height/16-1);
}
/**********
* 绘制标题
* @param
* @author 李鸿
**********/
public void DrawTitle(Graphics g){
g.setColor(0x000000);
g.setFont(font);
g.drawString(title, 0, 0, 0);
}
/**********
* 绘制列表中的图片
* @param
* @author 李鸿
**********/
public void DrawImage(Graphics g){
for(int i=0,j=height/16;i<imgListSize;i++,j+=(height/16)){
g.drawImage((Image)imgList.elementAt(i), 3, j+addValue, 0);
}
}
/**********
* 绘制列表中的字符串
* @param
* @author 李鸿
**********/
public void DrawList(Graphics g){
g.setColor(0x000000);
g.setFont(font);
for(int i=0,j=height/16;i<strListSize;i++,j+=height/16){
g.drawString((String)strList.elementAt(i), 3+imgWidth, j+addValue, 0);
}
}
/**********
* 绘制选择条
* @param
* @author 李鸿
**********/
public void DrawSelctToolBar(Graphics g){
if(imgListSize==0){
g.setColor(126,126,126);
g.drawRect(0, selectY, width, font.getHeight()+1);
g.setColor(72,178,240);
g.fillRect(1, selectY+1, width-1, font.getHeight());
}else{
g.setColor(126,126,126);
g.drawRect(0, selectY, width, imgHeight+1);
g.setColor(72,178,240);
g.fillRect(1, selectY+1, width-1, imgHeight);
}
}
/**********
* 绘制滚动条
* @param
* @author 李鸿 www.97xxoo.org
**********/
public void DrawScrollBar(Graphics g){
g.setColor(160,160,160);
g.fillRect(width-3, height/16, 3, (height-height/ );
g.setColor(0,100,200);
if(strListSize!=0){
g.fillRect(width-3, location*(height-height/ /strListSize, 3, (height-height/ /strListSize);
}
else{
return;
}
}
/**********
* 绘制底部
* @param
* @author 就去干李鸿
**********/
public void DrawBottom(Graphics g){
g.setColor(126,126,126);
g.drawRect(0, height-height/16, width, height/16);
g.setColor(72,178,240);
g.fillRect(1, height-height/16+1, width-1,height/16-1);
g.setColor(250,250,250);
g.drawLine(1, height-height/16+2, width-1, height-height/16+2);
}
/**********
* 初始化
* @param
* @author 李鸿
**********/
public void initList(){
this.width = this.getWidth();
this.height = this.getHeight();
selectY = height/16;
}
/**********
* 自动绘制方法
* @param
* @author 李鸿
**********/
protected void paint(Graphics g) {
// TODO Auto-generated method stub
DrawBackGround(g);
DrawTitle(g);
DrawSelctToolBar(g);
DrawScrollBar(g);
DrawImage(g);
DrawList(g);
DrawHead(g);
DrawBottom(g);
}
/**********
* 键盘事件
* @param
* @author 李鸿
**********
//此教程来源于97xxoo教程网(www.97xxoo.org)
//查看完整的教程请点:http://www.97xxoo.org/article/3/2008/20081116356.shtml
/
protected void keyPressed(int keyCode){
int keyAction = getGameAction(keyCode);
switch (keyAction) {
case Canvas.UP:
if (location >1) {
if (selectY <= height / 16) {
addValue += (height / 16);
} else {
selectY -= (height / 16);
}
location--;
selectIndex = location - 1;
}else{
if(strListSize<=14){
selectY = strListSize*(height / 16);
location = strListSize;
selectIndex = location - 1;
}else{
selectY = (height - 2 * height / 16);
location = strListSize;
selectIndex = location - 1;
addValue = -(location-14)*(height / 16);
}
}
break;
case Canvas.DOWN:
if (location < strListSize) {
if (selectY >= (height - 2 * height / 16)) {
addValue -= (height / 16);
} else {
selectY += (height / 16);
}
location++;
selectIndex = location - 1;
}else{
selectY = height / 16;
location = 1;
selectIndex = location - 1;
addValue = 0;
}
break;
case Canvas.LEFT:
break;
case Canvas.RIGHT:
break;
case Canvas.FIRE:
System.out.println(getSelectIndex());
break;
}
repaint();
}
} |
|