📄 sdalistview.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package cn.sda.ui;import cn.sda.event.KeybordEvent;import cn.sda.event.ListViewSelectChangeEvent;import cn.sda.event.ListViewSelectItemEvent;import cn.sda.event.PointerEvent;import java.util.Vector;import javax.microedition.lcdui.Graphics;import javax.microedition.lcdui.Image;import javax.microedition.lcdui.Font;/** * @author not attributable * @version 1.0 */public class SDAListView extends SDABaseControl { //边框 private int borderStyle = SDAConsts.bsFixedSingle; //滚动边框(多行) private int scrollBars = SDAConsts.srNone; private int barwidth = 10; //边框线颜色 private int borderColor = SDAConsts.clBlack; //滚动条颜色 private int scrollBarColor = SDAConsts.clBtnFace; //选中的项目的颜色 private int itemFocusedRectColor = SDAConsts.clBtnShadow; private int itemFocusedFontColor = SDAConsts.clActiveCaption; //列 private int colCount = 3; //行 private int rowHeight = 40; private boolean autoRowHeight = false; //内部变量 //行数,行最大长度 private int maxLenght = 0; private int maxHeight = 0; //垂直开始显示位置 private int startTop = 0; //水平开始显示位置 private int startLeft = 0; //变动前位置 private int oldStartLine = 0; private int oldStartLeft = 0; //水平滚条位置 private int HSLeft = 0; private int HSTop = 0; private int HSWidth = 0; private int HSHeight = 0; //垂直滚动条位置 private int VSLeft = 0; private int VSTop = 0; private int VSWidth = 0; private int VSHeight = 0; //点击的滚动条的位置 private int oldScrollPointx = 0; private int oldScrollPointy = 0; //按下滚动条 private boolean isscrollbarpointdown = false; //记录按下的滚动条0:水平,1:垂直的 private byte scrollbardownHV = 0; //项目管理 private Vector itemList = null; //但前选中项 private SDAListViewItem curItem = null; //最大列,最大行 private int maxCol = 0; private int maxRow = 0; //是否显示文字 private boolean showItemText = false; private boolean showItemRect = true; //事件 private ListViewSelectChangeEvent onSelectChange = null; private ListViewSelectItemEvent onSelectItem = null; public SDAListView() { super(); internalSDAListView(); } private void internalSDAListView() { itemList = new Vector(); this.setVisible(true); setLeft(0); setTop(0); setBorderStyle(SDAConsts.bsFixedSingle); super.setWidth(160); setHeight(100); super.setBackColor(SDAConsts.clWhite); this.onPointerPressed = new PointerEvent() { public void Event(SDABaseControl ctrl, int x, int y) { doPointerPressed(x, y); } }; this.onPointerReleased = new PointerEvent() { public void Event(SDABaseControl ctrl, int x, int y) { doPointerReleased(x, y); } }; this.onKeyUp = new KeybordEvent() { public void Event(SDABaseControl ctrl, int keyCode) { doKeyUp(keyCode); } }; } //画 public void paint() { internalPaint(); } private void internalPaint() { if (!IsCanPaint()) { return; } Graphics g = form.getGraphics(); g.setFont(getFont()); InternalPaint(g); } protected void InternalPaint(Graphics g) { //可视下才画 if (isVisible()) { //初始化引用变量 int thisWidth = getWidth(); int thisHeight = getHeight(); int HBarWidth = getHBarWidth(); int VBarHeight = getVBarHeight(); //对齐方式处理后,画组件 //设置字体 g.setFont(super.getFont()); //外框 SetClip(g); //透明 if (!isTransparent()) { g.setColor(backColor); fillRect(g, 0, 0, thisWidth, thisHeight); } if (borderStyle == SDAConsts.bsFixed3D) { g.setColor(SDAConsts.clGray); drawLine(g, 0, 0, thisWidth, 0); drawLine(g, 0, 0, 0, thisHeight); g.setColor(SDAConsts.clBlack); drawLine(g, 1, 1, thisWidth - 1, 1); drawLine(g, 1, 1, 1, thisHeight - 1); g.setColor(SDAConsts.clBtnFace); drawLine(g, 2, thisHeight - 1, thisWidth - 2, thisHeight - 1); drawLine(g, thisWidth - 1, 2, thisWidth - 1, thisHeight - 1); g.setColor(SDAConsts.clWhite); drawLine(g, 1, thisHeight, thisWidth, thisHeight); drawLine(g, thisWidth, 1, thisWidth, thisHeight); if (isFoucsed()) { g.setColor(SDAConsts.clFocusShadow); drawRect(g, 1, 1, thisWidth - 2, thisHeight - 2); } } if (borderStyle == SDAConsts.bsFixedSingle) { //边框 g.setColor(this.getBorderColor()); drawRect(g, 0, 0, thisWidth, thisHeight); if (isFoucsed()) { g.setColor(SDAConsts.clFocusShadow); drawRect(g, 1, 1, thisWidth - 2, thisHeight - 2); } } //画项目 paintItems(g); //滚动条 if (borderStyle == SDAConsts.bsFixed3D) { SetClip(g, 2, 2, thisWidth - 2, thisHeight - 2); } else { SetClip(g, 1, 1, thisWidth - 1, thisHeight - 1); } if ((scrollBars == SDAConsts.srHorizontal) || (scrollBars == SDAConsts.srBoth)) { //水平 g.setColor(scrollBarColor); fillRect(g, 0, thisHeight - barwidth, barwidth, barwidth); fillRect(g, HBarWidth - barwidth, thisHeight - barwidth, barwidth, barwidth); g.setColor(getBorderColor()); drawRect(g, 0, thisHeight - barwidth, barwidth, barwidth); drawRect(g, HBarWidth - barwidth, thisHeight - barwidth, barwidth, barwidth); //滚动块 g.setColor(backColor); fillRect(g, barwidth, thisHeight - barwidth, HBarWidth - 2 * barwidth, barwidth); g.setColor(this.getBorderColor()); drawRect(g, barwidth, thisHeight - barwidth, HBarWidth - 2 * barwidth, barwidth); //块大小 int pwidth = HBarWidth - 2 * barwidth; if (maxLenght > HBarWidth) { pwidth = ((HBarWidth) * (HBarWidth - 2 * barwidth)) / maxLenght; } //计算块位置 int ppos = (startLeft * (HBarWidth - 2 * barwidth)) / maxLenght; //修正位置 if (ppos + pwidth > HBarWidth - 2 * barwidth) { ppos = HBarWidth - 2 * barwidth - pwidth; } //画块 g.setColor(scrollBarColor); fillRect(g, barwidth + ppos, thisHeight - barwidth, pwidth, barwidth); g.setColor(getBorderColor()); drawRect(g, barwidth + ppos, thisHeight - barwidth, pwidth, barwidth); //记录大小 HSLeft = barwidth + ppos; HSTop = thisHeight - barwidth; HSWidth = pwidth; HSHeight = barwidth; //画块上的线 int tpos = HSWidth / 2 + HSLeft; drawRect(g, tpos, HSTop + 2, 0, HSHeight - 4); if (tpos - 2 > HSLeft) { drawRect(g, tpos - 2, HSTop + 2, 0, HSHeight - 4); } if (tpos + 2 < HSLeft + HSWidth) { drawRect(g, tpos + 2, HSTop + 2, 0, HSHeight - 4); } //三角 g.setColor(getBorderColor()); fillTriangle(g, barwidth / 2 - 2, thisHeight - barwidth / 2, barwidth / 2 + 2, thisHeight - barwidth / 2 - 4, barwidth / 2 + 2, thisHeight - barwidth / 2 + 4); fillTriangle(g, HBarWidth - barwidth + barwidth / 2 + 2, thisHeight - barwidth / 2, HBarWidth - barwidth + barwidth / 2 - 2, thisHeight - barwidth / 2 - 4, HBarWidth - barwidth + barwidth / 2 - 2, thisHeight - barwidth / 2 + 4); } if ((scrollBars == SDAConsts.srVertical) || (scrollBars == SDAConsts.srBoth)) { //垂直 g.setColor(scrollBarColor); fillRect(g, thisWidth - barwidth, 0, barwidth, barwidth); fillRect(g, thisWidth - barwidth, VBarHeight - barwidth, barwidth, barwidth); g.setColor(getBorderColor()); drawRect(g, thisWidth - barwidth, 0, barwidth, barwidth); drawRect(g, thisWidth - barwidth, VBarHeight - barwidth, barwidth, barwidth); //滚动块 g.setColor(backColor); fillRect(g, thisWidth - barwidth, barwidth, barwidth, VBarHeight - 2 * barwidth); g.setColor(getBorderColor()); drawRect(g, thisWidth - barwidth, barwidth, barwidth, VBarHeight - 2 * barwidth); //块大小 int pheight = VBarHeight - 2 * barwidth; if (maxHeight > VBarHeight) { pheight = ((VBarHeight) * (VBarHeight - 2 * barwidth) / maxHeight); } //计算块位置 int ppos = barwidth; if (maxHeight > 0) { ppos = (startTop * (VBarHeight - 2 * barwidth)) / maxHeight; } //修正位置 if (ppos + pheight > VBarHeight - 2 * barwidth) { ppos = VBarHeight - 2 * barwidth - pheight; } //画块 g.setColor(scrollBarColor); fillRect(g, thisWidth - barwidth, barwidth + ppos, barwidth, pheight); g.setColor(getBorderColor()); drawRect(g, thisWidth - barwidth, barwidth + ppos, barwidth, pheight); //记录大小 VSLeft = thisWidth - barwidth; VSTop = barwidth + ppos; VSWidth = barwidth; VSHeight = pheight; //画块上的线 int tpos = VSHeight / 2 + VSTop; drawRect(g, VSLeft + 2, tpos, barwidth - 4, 0); if (tpos - 2 > VSTop) { drawRect(g, VSLeft + 2, tpos - 2, VSWidth - 4, 0); } if (tpos + 2 < VSTop + VSHeight) { drawRect(g, VSLeft + 2, tpos + 2, VSWidth - 4, 0); } //三角 g.setColor(getBorderColor()); fillTriangle(g, thisWidth - barwidth + barwidth / 2, barwidth / 2 - 2, thisWidth - barwidth + barwidth / 2 - 4, barwidth / 2 + 2, thisWidth - barwidth + barwidth / 2 + 4, barwidth / 2 + 2); fillTriangle(g, thisWidth - barwidth + barwidth / 2 - 4, VBarHeight - barwidth + barwidth / 2 - 2, thisWidth - barwidth + barwidth / 2 + 4, VBarHeight - barwidth + barwidth / 2 - 2, thisWidth - barwidth + barwidth / 2, VBarHeight - barwidth / 2 + 2); } PaintChilds(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -