J2ME菜单的分页显示

本例由三个类组成:TestMidlet.java  TestCanvas.java ParseCode.java 简单的展示如何在J2ME里实现分页效果

以下代码可以直接运行
入口类:TestMidlet.java

import  javax.microedition.lcdui.Display;
import  javax.microedition.lcdui.Displayable;
import  javax.microedition.midlet.MIDlet;
import  javax.microedition.midlet.MIDletStateChangeException;

public   class  TestMidlet  extends  MIDlet  {
    
private static TestMidlet _instance = null;

    
private Display _display = null;

    
public TestMidlet() {
        _instance 
= this;
        _display 
= Display.getDisplay(this);
    }


    
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
    }


    
protected void pauseApp() {
    }


    
protected void startApp() throws MIDletStateChangeException // 启动程序
        forward(new TestCanvas());
    }


    
public static TestMidlet getInstance() {
        
return _instance;
    }

    
    
public static void forward(Displayable next){
        _instance._display.setCurrent(next);
    }


}

 

主屏幕:TestCanvas.java

 

import  java.util.Vector;

import  javax.microedition.lcdui.Alert;
import  javax.microedition.lcdui.AlertType;
import  javax.microedition.lcdui.Canvas;
import  javax.microedition.lcdui.Font;
import  javax.microedition.lcdui.Graphics;

public   class  TestCanvas  extends  Canvas  implements  Runnable {
    
static Vector v = null;                                // 菜单项
    static final Font _LowFont = Font.getFont(Font.FACE_MONOSPACE,
            Font.STYLE_PLAIN, Font.SIZE_MEDIUM);         
// 字体类型
    static final int _LowColor = 0x000000FF;             // 未选中颜色
    private static int lowFont_Height;
    
static final int _HightColor = 0x000000FF;             // 选中颜色
    static final int _HightBGColor = 0x00CCCCCC;         // 背景颜色

    
static int width;
    
static int height; 
    
static int startHeight;                             // 菜单在屏幕的起始高度
    static int menuIndex;                                 // 业务类型菜单选中项的序号
    static final int spacing = _LowFont.getHeight() / 2;// 计算两行字之间的空白距离

    
private static String title = null;
    
private static int twidth;
    
private static int tleft_start;
    
public static String curMenuText = null;             // 当前的名字
    public static String curMenuType = null;            // 当前的武力
    public static String menuType = null;                 // 武力
    public static String menuText = null;                 // 名字
    private static ParseCode pc = null;                    

    Thread menuThread;
    
private static int numPerPage;                        // 每页显示的内容数量
    private static int totalPage;                        // 总页数
    private static int curPage;                            // 当前的页数
    private static int curItem = 0;                        // 当前页面显示的最前一条记录位置
    private static int curMax;                            // 当前页面显示的最后一条记录位置
    private static boolean hasInit;                        // 是否初始化了

    
public TestCanvas() {
        
if(!hasInit){
            width 
= getWidth();
            height 
= getHeight();

            pc 
= new ParseCode();
            String test 
= "98|赵云#98|马超#99|张飞#98|关羽#97|许褚#" +
                    
"96|夏候惇#91|夏候渊#98|典韦#97|黄忠#100|吕布#" +
                    
"97|太史慈#96|甘宁#95|孙策#72|周瑜#93|姜维#";
            v 
= pc.parseString(test);

            curPage 
= 1;

            title 
= "请选择武将";
            twidth 
= _LowFont.stringWidth(title);
            tleft_start 
= (width - twidth) / 2;
            lowFont_Height 
= _LowFont.getHeight();

            startHeight 
= lowFont_Height;
            numPerPage 
= (height - startHeight - spacing)/(lowFont_Height );
            
if(numPerPage < v.size()){
                totalPage 
= v.size() / numPerPage;
                
if(v.size() % numPerPage != 0) totalPage++;
            }

            
else{
                numPerPage 
= v.size();
                totalPage 
= 1;
            }


            hasInit 
= true;
        }


        menuIndex 
= 0;

        menuThread 
= new Thread(this);
        menuThread.start();
    }


    
public void run() {
        
while (true{
            repaint();
        }

    }


    
protected void paint(Graphics g) {
        g.setColor(
0x00FFFFFF);
        g.fillRect(
00, width, height);

        
if(numPerPage * curPage > v.size())
            curMax 
= v.size();
        
else
            curMax 
= numPerPage * curPage;
        curItem 
= numPerPage * (curPage - 1);

        
for(int i = curItem; i < curMax; i++){
            
int tmpHeight = startHeight + (i - curItem) * lowFont_Height + spacing;    
            String str 
= (String)v.elementAt(i);

            
int pos = str.indexOf("|");
            
if(pos != -1){
                menuType 
= str.substring(0, pos);
                menuText 
= str.substring(pos+1, str.length());
            }
else{
                menuType 
= null;
                menuText 
= str;
            }

            
int tmpWidth = (width - _LowFont.stringWidth(menuText)) / 2;
            
if ((i - curItem) == menuIndex) {
                curMenuType 
= menuType;
                curMenuText 
= menuText;
                g.setColor(_HightColor);
                g.fillRect(tmpWidth, tmpHeight, _LowFont.stringWidth(curMenuText),
                        lowFont_Height);
                g.setFont(_LowFont);
                g.setColor(_HightBGColor);
                g.drawString(curMenuText, tmpWidth, tmpHeight, 
20);

            }
 else {
                g.setFont(_LowFont);
                g.setColor(_LowColor);
                g.drawString(menuText, tmpWidth, tmpHeight, 
20);
            }
                
        }


        
if(curPage < totalPage){        // 还有下一页
            String next = "-->";
            g.setFont(_LowFont);
            g.setColor(_LowColor);
            g.drawString(next, width
-_LowFont.stringWidth(next), height - lowFont_Height, 20);
        }


        
if(curPage > 1){                // 还有上一页
            String next = "<--";
            g.setFont(_LowFont);
            g.setColor(_LowColor);
            g.drawString(next, 
1, height - lowFont_Height, 20);
        }


        g.setColor(_LowColor);
        g.fillRect(
00, width, lowFont_Height);

        g.setFont(_LowFont);
        g.setColor(
0x00FFFFFF);
        g.drawString(title, tleft_start, 
020);
    }


    
protected void keyPressed(int code) {
        
if (getGameAction(code) == Canvas.UP & (curItem + menuIndex) > curItem) {
            menuIndex
--;                         // 按上时且不是第一项, 菜单项序号减1
        }
 else if (getGameAction(code) == Canvas.DOWN & (curItem + menuIndex ) < (curMax - 1)) {
            menuIndex
++;                         // 按下时且不是最后一项时, 菜单项序号加1
        }
 else if (getGameAction(code) == Canvas.FIRE) // 响应摇杆选中事件
            TestMidlet.forward(new Alert("您选中的武将""武力:" + curMenuType + " 姓名:" + curMenuText, null, AlertType.INFO));
        }
 else if (getGameAction(code) == Canvas.RIGHT & (curPage < totalPage)){
            curPage 
++;
            TestMidlet.forward(
new TestCanvas());
        }
 else if(getGameAction(code) == Canvas.LEFT & (curPage > 1)){
            curPage 
--;
            TestMidlet.forward(
new TestCanvas());
        }

    }


}

 

解析并获取特定格式的字符串:ParseCode.java

 


import  java.io.ByteArrayInputStream;
import  java.io.ByteArrayOutputStream;
import  java.io.IOException;
import  java.io.InputStream;
import  java.io.UnsupportedEncodingException;
import  java.util.Vector;

public   class  ParseCode  {

    
public Vector parseString(String str){
        
int c = 0;
        String content 
= null;
        Vector v 
= new Vector();
        InputStream is 
= null;
        ByteArrayOutputStream baos 
= null;

        
try {
            is 
= new ByteArrayInputStream(str.getBytes("UTF-8"));
            baos 
= new ByteArrayOutputStream(); 
            
while ((c = is.read()) != -1{
                
switch (c) {
                
case '#':
                    content 
= null;
                    content 
= new String(baos.toByteArray(), "UTF-8");
                    v.addElement(content);
                    baos.reset();
                    
break;
                
default:
                    baos.write((
char) c);
                
break;
                }

            }

        }
 catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
 catch (IOException e) {
            e.printStackTrace();
        }
 finally{
            
try{
                
if(is != null)
                    is.close();
                
if(baos != null)
                    baos.close();
            }
catch(IOException ioe){
                ioe.printStackTrace();
            }

        }

        
return v;
    }

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值