当前位置: 代码迷 >> J2ME >> j2me翻页例证
  详细解决方案

j2me翻页例证

热度:6400   发布时间:2013-02-25 21:31:32.0
j2me翻页例子
//执行类
public class Test extends MIDlet{    
private Display display;    
private TextCanvas tcanvas;    
  
public Test() {    
// TODO Auto-generated constructor stub    
display=Display.getDisplay(this);    
tcanvas=new TextCanvas(this);    
}    
  
protected void startApp() throws MIDletStateChangeException {    
display.setCurrent(tcanvas);    
  
}    
  
protected void pauseApp() {    
// TODO Auto-generated method stub    
  
}    
  
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {    
// TODO Auto-generated method stub    
  
}    
  
}    
  

  //翻页实现类

public class TextCanvas extends Canvas {    
    
InputStream is;    
InputStreamReader ir;    
int b=0,page=1,num=-1;    
private int count=0;    
private String str=new String();    
private String ss=new String();   
private static final int MAX_SIZE=10;    
    
    
public TextCanvas(Test test) {    
  str=this.getText();    
  System.out.println("--text.size--"+str.length());   
}    
  
protected void paint(Graphics g) {    
// TODO Auto-generated method stub    
  int x = 0;   
        int y = 0;   
  //设置画布颜色   
//  g.setColor(0xD8d8BF);   
//     g.fillRect(0,0,this.getWidth(),this.getHeight());     
             
          g.setColor(255, 255, 255);   
       g.fillRect(0, 0, getWidth(), this.getHeight());    
            
       ss=this.TurnPage(count);   
       g.setColor(0x000000);//设置内容颜色   
       g.drawString(ss, x, y,Graphics.LEFT|Graphics.TOP);   
}    
  
protected void keyPressed(int keyCode){    
     
//4向前翻页,6向后翻页;    
if(keyCode==Canvas.KEY_NUM6){    
System.out.println("-------6向后翻页--------");    
this.changePage(page);    
page++;   
this.repaint();   
}    
else if(keyCode==Canvas.KEY_NUM4){    
System.out.println("---------4向前翻页----------");    
this.changePage(page-1);    
page--;   
this.repaint();   
}    
}   
    
//翻页功能pageNum==1时向下翻一页,pageNum==-1时向上翻一页    
public int changePage(int pageNum){    
  
System.out.println("--前count--/"+count+"----pageNume----/"+pageNum);    
if(page>=1){    
count=MAX_SIZE*pageNum;    
} else if(page <1){    
count=0;    
}    
System.out.println("--后count--/"+count);    
      return count;   
}    
  
//截取当前页字符串    
protected String TurnPage(int start){    
String temp="";    
int end=start+MAX_SIZE;   
int ks=start;   
System.out.println(ks+"/"+end);   
if(end<=str.length()){   
temp=str.substring(ks,end);    
}else if(ks<str.length()){   
  temp=str.substring(ks);   
}else{   
  temp="文本已读完!!";   
}   
System.out.println("------str---"+temp+"-currentPage--/"+page);   
return temp;    
}    
  
    
// 获取文件中的字符将其放入字符串对象中    
protected String getText(){    
  
is = getClass().getResourceAsStream("/a.txt");    
ir=new InputStreamReader(is);    
  
if (null != is) {    
int ch = 0;    
try {    
while ((ch = ir.read()) != -1) {    
str=str.concat(String.valueOf((char)ch));    
}    
} catch (IOException e) {    
e.printStackTrace();    
}    
// System.out.println("-------获取文件中的字符---"+str);    
return str;    
} else {    
return null;    
}    
}    
  
    
}