现在的位置: 首页 > 综合 > 正文

J2ME菜单的分页显示

2013年10月07日 ⁄ 综合 ⁄ 共 5208字 ⁄ 字号 评论关闭

本例由三个类组成: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 

抱歉!评论已关闭.