?? tableitem.java
字號:
package itemex;
import javax.microedition.lcdui.*;
public class TableItem extends CustomItem
{
private int rows, cols;//保存表格內行列數
private int dx, dy;//保存單元格大小
private int currentX ,currentY ;//保存當前選中位置
private String[ ][ ] data; //保存各個單元格文字
private int maxWidth, maxHeight;//保存當前對象要求大小
private int curWidth, curHeight;//保存當前對象實際大小
private boolean isShown=false; //保存當前是否處于顯示狀態
private boolean isIn=false;//保存當前是否處于焦點狀態
//參數 label:標題
//c,r:表格的列數和行數
//w,h: 單元格的寬度和高度
public TableItem(String label, int c, int r, int w, int h)
{
super(label);
currentX = currentY = 0;
rows = r;
cols = c;
data = new String[rows][cols];
dx =w;
dy =h;
maxWidth=dx* cols+1;
maxHeight=dy* rows+1;
isShown = false;
}
public void setCell(int w, int h)
{//設置指定位置單元格的寬度和高度,同時重新計算對象大小
dx =w;
dy =h;
maxWidth=dx* cols+1;
maxHeight=dy* rows+1;
invalidate();//要求
}
public int getCurrentX()
{//得到當前選中的單元格的X坐標
return currentX;
}
public int getCurrentY()
{//得到當前選中的單元格的Y坐標
return currentY;
}
public String getText(int x,int y)
{//得到單元格的文字
if( data[y][x] ==null)
data[y][x]="";
return data[y][x];
}
public void setText(String text,int x, int y)
{//設置指定位置單元格的文字,同時刷新單元格區域
data[y][x] = text;
if( isShown)
repaint(x * dx, y * dy, dx, dy);
}
//計算尺寸要求的相關重載函數
protected int getMinContentHeight()
{
System.out.println("getMinContentHeight return "+ maxHeight);
return maxHeight;
}
protected int getMinContentWidth()
{
System.out.println("getMinContentWidth return "+ maxWidth);
return maxWidth;
}
protected int getPrefContentHeight(int width)
{
System.out.println("getPrefContentHeight return "+ maxHeight);
return maxHeight;
}
protected int getPrefContentWidth(int height)
{
System.out.println("getPrefContentWidth return "+ maxWidth);
return maxWidth;
}
//尺寸變化時的通知函數
protected void sizeChanged(int w, int h)
{
System.out.println("sizeChanged (w,h)=("+ w +","+ h +")");
curWidth = w;
curHeight = h;
}
//顯示與隱藏時的通知函數
protected void showNotify()
{
System.out.println("showNotify");
isShown = true;
}
protected void hideNotify()
{
System.out.println("hideNotify");
isShown = false;
}
//重繪函數
protected void paint(Graphics g, int w, int h)
{
System.out.println("paint (w,h) ("+w+","+h+")");
g.setColor(0x00ffffff);//用白色填充整個區域
g.fillRect(0, 0, w, h);
g.setColor(0x00000000);//用黑色繪制線條
for (int i = 0; i <= rows; i++)
{//繪制橫線
g.drawLine(0, i * dy, cols * dx, i * dy);
}
for (int i = 0; i <= cols; i++)
{//繪制豎線
g.drawLine(i * dx, 0, i * dx, rows * dy);
}
g.setColor(0x00D0D0D0);//用灰色填充當前被選中單元格
g.fillRect((currentX * dx) + 1, (currentY * dy) + 1, dx - 1, dy - 1);
g.setColor(0x00000000);//用黑色顯示文字
for (int i = 0; i < rows; i++)
{//逐個繪制每個單元格
for (int j = 0; j < cols; j++)
{
if (data[i][j] != null)
{
g.setClip((j * dx) + 1, i * dy, dx - 1, dy - 1);
g.drawString(data[i][j], (j * dx) + 2, ((i + 1) * dy) - 2,
Graphics.BOTTOM | Graphics.LEFT);
}
}
}
}
protected boolean traverse(int dir, int viewportWidth, int viewportHeight, int[] visRect_inout)
{//處理方向鍵事件
System.out.println("viewport (w,h) ("+viewportWidth+","+viewportHeight +")");
System.out.println("visRect_in ("+visRect_inout[0]+","+visRect_inout[1]+","+visRect_inout[2]+","+visRect_inout[3]+")");
if(!isIn)
{//得到焦點
isIn = true;
return true;
}
switch (dir)
{
case Canvas.DOWN:
if (currentY < (rows - 1))
{
currentY++;
repaint(currentX * dx, (currentY - 1) * dy, dx, dy);
repaint(currentX * dx, currentY * dy, dx, dy);
}
else
return false;
break;
case Canvas.UP:
if (currentY > 0)
{
currentY--;
repaint(currentX * dx, (currentY + 1) * dy, dx, dy);
repaint(currentX * dx, currentY * dy, dx, dy);
}
else
return false;
break;
case Canvas.LEFT:
if (currentX > 0)
{
currentX--;
repaint((currentX + 1) * dx, currentY * dy, dx, dy);
repaint(currentX * dx, currentY * dy, dx, dy);
}
break;
case Canvas.RIGHT:
if (currentX < (cols - 1))
{
currentX++;
repaint((currentX - 1) * dx, currentY * dy, dx, dy);
repaint(currentX * dx, currentY * dy, dx, dy);
}
break;
}
visRect_inout[0] = currentX;
visRect_inout[1] = currentY;
visRect_inout[2] = dx;
visRect_inout[3] = dy;
System.out.println("visRect_out ("+visRect_inout[0]+","+visRect_inout[1]+","+visRect_inout[2]+","+visRect_inout[3]+")");
return true;
}
protected void traverseOut()
{
System.out.println("traverseOut");
isIn = false;
}
protected void keyPressed(int keyCode)
{
System.out.println("keyCode=" + keyCode);
if (data[currentY][currentX] == null)
data[currentY][currentX] ="";
if( keyCode == -8)// 清除鍵
{//刪除最后一個字符
int len =data[currentY][currentX].length();
if( len >0)
data[currentY][currentX] = data[currentY][currentX].substring(0, len-1);
}
else if( (char)keyCode>='0' && (char)keyCode<='9')
data[currentY][currentX]+= (char)keyCode; //添加到末尾
repaint(currentX * dx, currentY * dy, dx, dy);//重繪屏幕
}
protected void pointerPressed(int x,int y)
{
System.out.println("pointerPressed (x,y)= ("+x+","+y+")");
if( x>=0 && x<maxWidth && y>=0 && y<maxHeight)
{
currentX = x/dx;
currentY = y/dy;
repaint(currentX * dx, currentY * dy, dx, dy);//重繪屏幕
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -