📄 listbox.java
字号:
import java.util.*;
import javax.microedition.lcdui.*;
/*******************************************************************************
* 类功能:类似Windows列表框控件
******************************************************************************/
public class ListBox {
public final static int FONT_SIZE_SMALL = Font.SIZE_SMALL; // 小字体
public final static int FONT_SIZE_MEDIUM = Font.SIZE_MEDIUM; // 中字体
public final static int FONT_SIZE_LARGE = Font.SIZE_LARGE; // 大字体
private Vector itemVector;
private Font font;
private int listIndex; // 当前显示项的索引值
private int barColor = 0x0000FF; // 选择条颜色(蓝)
private int choicedFontColor = 0xFFFFFF; // 被选中文字颜色(白)
private int RGB[] = new int[3];//RGB模式下的调色;
private int fontColor = 0; // 字体颜色(黑)
private int fontHeight; // 字体高度
private int startY = 8; // 滚动标识所占纵座标大小
private boolean drawBackground = false; // 是否画背景
private boolean drawBorder = false; // 是否画边框
private boolean drawBar = false; // 是否画选择条
private boolean drawPointer = false;// 是否画指针图像;
private boolean empty = true; // 是否没有任何项
Image im;
/***************************************************************************
* 函数功能:构造函数 参数:无 返回值:
**************************************************************************/
public ListBox() {
itemVector = new Vector();
// 字体默认为不带任何状态的小字体
font = Font
.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, FONT_SIZE_SMALL);
// 默认显示第一项(从0开始)
listIndex = 0;
// 字体高度 = 字体原高度 + 行间距
fontHeight = font.getHeight() + 2;
for(int i=0;i<3;i++)
RGB[i]=0;//初始颜色为黑色;
}
public ListBox(Image im) {
this.im = im;
itemVector = new Vector();
// 字体默认为不带任何状态的小字体
font = Font
.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, FONT_SIZE_SMALL);
// 默认显示第一项(从0开始)
listIndex = 0;
// 字体高度 = 字体原高度 + 行间距
fontHeight = font.getHeight() + 2;
}
/***************************************************************************
* 函数功能:添加列表项(在末尾添加) 参数:列表项名称 返回值:无
**************************************************************************/
public void addItem(String itemName) {
itemVector.addElement(itemName);
empty = false;
return;
}
/***************************************************************************
* 函数功能:移除指定名称的列表项 参数:列表项名称 返回值:无
**************************************************************************/
public void removeItem(String itemName) {
itemVector.removeElement(itemName);
return;
}
/***************************************************************************
* 函数功能:移除指定位置的列表项 参数:列表项索引号 返回值:无
**************************************************************************/
public void removeItem(int index) {
itemVector.removeElementAt(index);
return;
}
/***************************************************************************
* 函数功能:在指定位置画出列表框 参数:Graphics对象, 座标x, 座标y, 列表框长度, 列表框高度 (以像素点为单位) 返回值:无
**************************************************************************/
public void draw(Graphics g, int pX, int pY, int width, int height) {
g.setClip(pX, pY, width + 1, height + 1);
// 画框
if (drawBorder) {
g.setColor(0);
g.drawRoundRect(pX, pY, width, height, 7, 7);
g.setColor(0xFFFFFF);
g.drawRoundRect(pX + 1, pY + 1, width - 2, height - 2, 7, 7);
g.setColor(175, 175, 175);
g.drawRoundRect(pX + 2, pY + 2, width - 4, height - 4, 7, 7);
g.setColor(0);
g.drawRoundRect(pX + 3, pY + 3, width - 6, height - 6, 7, 7);
}
// 画背景
if (drawBackground) {
g.setColor(24, 176, 8);
g.fillRoundRect(pX + 4, pY + 4, width - 7, height - 7, 7, 7);
g.setColor(16, 128, 0);
g.fillRoundRect(pX + 4, pY + 4, width - 7, 2, 7, 7);
g.fillRoundRect(pX + 4, pY + 4, 2, height - 7, 7, 7);
g.setColor(22, 224, 8);
g
.fillRoundRect(pX + 4 + width - 7 - 2, pY + 4, 2,
height - 7, 7, 7);
g
.fillRoundRect(pX + 4, pY + 4 + height - 7 - 2, width - 7,
2, 7, 7);
}
g.setClip(pX + 1, pY + startY, width - 1, height - startY * 2);
// 画选择条
if (drawBar) {
int posY;
// 如果被选文字超过列表框高度则把选择条画在最下面
if ((listIndex + 1) * fontHeight + pY + (startY * 2) > pY + height)
posY = pY + height - fontHeight - startY;
// 否则按正常画
else
posY = listIndex * fontHeight + pY + 1 + startY;
g.setColor(barColor);
g.fillRect(pX + 10, posY, width - 20, fontHeight);
}
// 画列表项内容
g.setFont(font);
int posY;
// 如果被选文字超过列表框高度则把所有文字上移
if (pY + (listIndex + 1) * fontHeight + (startY * 2) > pY + height)
// height / fontHeight 求列表框中最多显示的列数
// height % fontHeight 求列表框中未显示全完整的字占多少像素点
posY = -fontHeight * (listIndex - height / fontHeight + 1)
+ (height % fontHeight) - 1 - startY;
// 否则从最顶开始显示
else
posY = startY;
for (int i = 0; i < itemVector.size(); i++) {
if (i == listIndex) {
if (drawPointer) {
g.drawImage(im, pX + 12, pY + i * fontHeight + 6 + posY,
Graphics.LEFT | Graphics.TOP);
}
g.setColor(choicedFontColor);
} else
g.setColor(fontColor);
g.drawString((String) itemVector.elementAt(i), pX+20, pY + i
* fontHeight + 4 + posY, Graphics.LEFT | Graphics.TOP);//这个地方是关键,一次性画出所有的选择项,然后哪个高亮就用不同的颜色标注出来;
}
drawArrow(g, pX, pY, width, height, false, false);
return;
}
/***************************************************************************
* 函数功能:设置选择条颜色 参数:颜色值, 是否要画 返回值:无
**************************************************************************/
public void setBarColor(int color, boolean drawIt) {
barColor = color;
drawBar = drawIt;
}
/***************************************************************************
* 函数功能:设置被选择文字颜色 参数:颜色值, 是否要画 返回值:无
**************************************************************************/
public void setChoicedFontColor(int color, boolean drawIt) {
if (drawIt)
choicedFontColor = color;
else
choicedFontColor = fontColor;
}
/***************************************************************************
* 函数功能:设置列表框的字体颜色 参数:颜色值 返回值:无
**************************************************************************/
public void setFontColor(int color) {
fontColor = color;
}
/***************************************************************************
* 函数功能:向后移动选择项 参数:无 返回值:无
**************************************************************************/
public void moveNext() {
if (listIndex < itemVector.size() - 1)
listIndex++;
else
listIndex = 0;
}
/***************************************************************************
* 函数功能:向前移动选择项 参数:无 返回值:无
**************************************************************************/
public void movePrev() {
if (listIndex > 0)
listIndex--;
else
listIndex = itemVector.size() - 1;
}
/***************************************************************************
* 函数功能:设置行间距 参数:行间距大小 返回值:无
**************************************************************************/
public void setLineSpace(int lineSpace) {
fontHeight = font.getHeight() + lineSpace;
}
/***************************************************************************
* 函数功能:画滚动标识 参数:Graphics对象, 座标x, 座标y, 列表框长度, 列表框高度 (以像素点为单位) 返回值:无
**************************************************************************/
private void drawArrow(Graphics g, int pX, int pY, int width, int height,
boolean up, boolean down) {
boolean drawUp, drawDown;
drawUp = up;
drawDown = down;
// 是否画向上的箭头
// if((listIndex + 1) * fontHeight + pY + (startY * 2) > pY + height)
// drawUp = true;
// else
// drawUp = false;
//
// // 是否画向下的箭头
// if(height / fontHeight < itemVector.size() && listIndex !=
// itemVector.size() - 1)
// drawDown = true;
// else
// drawDown = false;
g.setClip(pX, pY, width, height);
g.setColor(0);
for (int i = 0; i < 7; i++) {
int x = pX + width / 2;
int y;
if (drawUp) {
y = pY + i + 1;
g.drawLine(x - i, y, x + i, y);
}
if (drawDown) {
y = pY + height - i - 1;
g.drawLine(x - i, y, x + i, y);
}
}
g.setColor(255, 255, 0);
for (int i = 0; i < 5; i++) {
int x = pX + width / 2;
int y;
if (drawUp) {
y = pY + i + 2;
g.drawLine(x - i, y, x + i, y);
}
if (drawDown) {
y = pY + height - i - 2;
g.drawLine(x - i, y, x + i, y);
}
}
}
/***************************************************************************
* 函数功能:取得当前显示项的索引值 参数:无 返回值:当前显示项的索引值(从0开始)
**************************************************************************/
public int getIndex() {
return listIndex;
}
/***************************************************************************
* 函数功能:清除所有项 参数:无 返回值:无
**************************************************************************/
public void clear() {
itemVector.removeAllElements();
empty = true;
listIndex = 0;
}
/***************************************************************************
* 函数功能:列表框是否为空 参数:无 返回值:是否为空
**************************************************************************/
public boolean isEmpty() {
return empty;
}
/***************************************************************************
* 函数功能:取得列表项总数 参数:无 返回值:列表项数
**************************************************************************/
public int getMaxItem() {
return itemVector.size();
}
/***************************************************************************
* 得到当前显示索引项的字符串内容;
****************************************************************************/
public String getItemString() {
return itemVector.elementAt(listIndex).toString();
}
/***************************************************************************
* 函数功能:设置字体大小 参数:字体大小 返回值:无
**************************************************************************/
public void setFontSize(int fontSize) {
font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, fontSize);
}
/***************************************************************************
* 函数功能:是否画边框 参数:布尔值 返回值:无
**************************************************************************/
public void setBorder(boolean isDraw) {
drawBorder = isDraw;
}
/***************************************************************************
* 函数功能:是否画背景 参数:布尔值 返回值:无
**************************************************************************/
public void setBackground(boolean isDraw) {
drawBackground = isDraw;
}
/***************************************************************************
* 函数功能:更改列表项的显示内容 参数:索引, 新列表项内容 返回值:无
**************************************************************************/
public void replaceItem(int index, String itemName) {
itemVector.setElementAt(itemName, index);
}
/*
*
*
*
* 是否画指针图像
*/
public void setPointer(boolean isDraw) {
drawPointer = isDraw;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -