您的位置:首页 > 其它

J2ME开发入门(二) 一个简单的分页

2007-01-08 15:18 218 查看



/**//*


* AbstractMenuForm.java


*


* Created on 2006年12月31日, 上午10:36


*


* To change this template, choose Tools | Options and locate the template under


* the Source Creation and Management node. Right-click the template and choose


* Open. You can then make changes to the template in the Source Editor.


*/




package edu.fjtu.ui;


import java.util.Vector;




import javax.microedition.lcdui.Canvas;


import javax.microedition.lcdui.Font;


import javax.microedition.lcdui.Graphics;








/** *//**


*


* @author gaobin


* 自定义菜单窗体


* 只需要输入 字符串就可以了


*


*/




public class AbstractMenuForm extends Canvas...{




//set selected and unselected font


private Font lowFont = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_SMALL);


private Font highFont = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, Font.SIZE_MEDIUM);




//1 is unselected color


//2 is selected color


//3 is selected background color


private int lowColor = 0x000000FF;


private int highColor = 0x00FF0000;


private int highBGColor = 0x00CCCCCC;






private int startHeight = 10;


private int fontmaxwidth = 20;


private int spacing = 2; // spacing between menu items


private int appearNum = 6;//define number menu appear on desk定义桌面上显示的菜单项数




private int width;


private int height;


protected int menuIdx = 0;//当前选中位置


private int menuCount;


protected Vector strVector;






public AbstractMenuForm(Vector strVector1) ...{


width = getWidth();


height = getHeight();


strVector = new Vector();


strVector = strVector1;


menuCount = strVector.size();


}






public void paint(Graphics g)...{


g.setColor(188,223,155);


g.fillRect(0,0,width,height);


g.setColor(0,0,0);




int tempnum = appearNum>menuCount?menuCount:appearNum;


paint1(g,tempnum);


}








/**//* 超复杂


* 自己看吧 看不懂也没关系-,-


* 反正只要接受一个 全部包含字符串的VECTOR就可以了


* */




public void paint1(Graphics g,int tempnum)...{




for(int i=0;i<tempnum;i++) ...{






if(tempnum-menuIdx>0) ...{


String s = (String)strVector.elementAt(i);




if (i==menuIdx) ...{


g.setColor(highBGColor);


g.fillRect(0,startHeight + (highFont.getHeight()+spacing)*i,highFont.stringWidth(s),highFont.getHeight());


g.setFont(highFont);


g.setColor(highColor);


g.drawString(s,0,startHeight + (highFont.getHeight()+spacing)*i,Graphics.LEFT|Graphics.TOP);




} else ...{


g.setFont(lowFont);


g.setColor(lowColor);


g.drawString(s,0,startHeight + (highFont.getHeight()+spacing)*i,Graphics.LEFT|Graphics.TOP);


}




}else...{


int j = menuIdx-tempnum+i+1;


String s = (String)strVector.elementAt(j);




if (j==menuIdx) ...{


g.setColor(highBGColor);


g.fillRect(0,startHeight + (highFont.getHeight()+spacing)*(tempnum-1),highFont.stringWidth(s),highFont.getHeight());


g.setFont(highFont);


g.setColor(highColor);


g.drawString(s,0,startHeight + (highFont.getHeight()+spacing)*(tempnum-1),Graphics.LEFT|Graphics.TOP);




} else ...{


g.setFont(lowFont);


g.setColor(lowColor);


g.drawString(s,0,startHeight + (highFont.getHeight()+spacing)*i,Graphics.LEFT|Graphics.TOP);


}


}


}


}






protected void keyPressed(int code) ...{




if (getGameAction(code) == Canvas.UP && menuIdx - 1 >= 0) ...{


menuIdx--;




} else if (getGameAction(code) == Canvas.DOWN && menuIdx + 1 < menuCount) ...{


menuIdx++;


}


repaint();


}




//public abstract void initstrVector();








//所有的GET SET方法




public Font getLowFont() ...{


return lowFont;


}






public void setLowFont(Font lowFont) ...{


this.lowFont = lowFont;


}






public Font getHighFont() ...{


return highFont;


}






public void setHighFont(Font highFont) ...{


this.highFont = highFont;


}






public int getLowColor() ...{


return lowColor;


}






public void setLowColor(int lowColor) ...{


this.lowColor = lowColor;


}






public int getHighColor() ...{


return highColor;


}






public void setHighColor(int highColor) ...{


this.highColor = highColor;


}






public int getHighBGColor() ...{


return highBGColor;


}






public void setHighBGColor(int highBGColor) ...{


this.highBGColor = highBGColor;


}






public int getStartHeight() ...{


return startHeight;


}






public void setStartHeight(int startHeight) ...{


this.startHeight = startHeight;


}






public int getFontmaxwidth() ...{


return fontmaxwidth;


}






public void setFontmaxwidth(int fontmaxwidth) ...{


this.fontmaxwidth = fontmaxwidth;


}






public int getSpacing() ...{


return spacing;


}






public void setSpacing(int spacing) ...{


this.spacing = spacing;


}






public int getAppearNum() ...{


return appearNum;


}






public void setAppearNum(int appearNum) ...{


this.appearNum = appearNum;


}




}



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: