j2me菜单设计方案

//文件名 SimpleCustomMenu.java

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;

public class SimpleCustomMenu extends MIDlet implements CommandListener {

Display display;
Display pauseDisplay;
boolean isSplash = true;
MenuScreen menuScreen;

public SimpleCustomMenu() {
MenuScreen menuScreen = new MenuScreen();
display = Display.getDisplay(this);
display.setCurrent(menuScreen);
}

protected void startApp() throws MIDletStateChangeException {
}

protected void pauseApp() {}

protected void destroyApp(boolean flag) throws MIDletStateChangeException {}

public void commandAction(Command cmd, Displayable dis) {

}
}
////////////////////////////////////////////////////
//文件名 MenuScreen.java

import javax.microedition.lcdui.*;

public class MenuScreen extends Canvas implements Runnable {

    static final Font lowFont = Font.getFont(Font.FACE_MONOSPACE,
                                             Font.STYLE_PLAIN, Font.SIZE_SMALL);
    static final Font highFont = Font.getFont(Font.FACE_MONOSPACE,
                                              Font.STYLE_BOLD, Font.SIZE_MEDIUM);

    static final int lowColor = 0x000000FF;
    static final int highColor = 0x00FF0000;
    static final int highBGColor = 0x00CCCCCC;

    static int width;
    static int height;

    static int startHeight;

    static final int spacing = highFont.getHeight() / 2;

    static final int NEW_GAME = 0;
    static final int HIGH_SCORE = 1;
    static final int SETTINGS = 2;
    static final int HELP = 3;
    static final int ABOUT = 4;
    static final int MENU_ITEM_COUNT = 5;
    static String[] mainMenu = new String[MENU_ITEM_COUNT];

    static int menuIdx;

    Thread menuThread;

    public MenuScreen() {
        width = getWidth();
        height = getHeight();

        mainMenu[0] = "开始新游戏";
        mainMenu[1] = "显示得分";
        mainMenu[2] = "设置";
        mainMenu[3] = "帮助";
        mainMenu[4] = "关于";

        startHeight = (highFont.getHeight() * mainMenu.length) +
                      ((mainMenu.length - 1) * spacing);
        startHeight = (height - startHeight) / 2;

        menuIdx = 0;

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

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

    public void paint(Graphics g) {

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

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

            if (i == menuIdx) {
                g.setColor(highBGColor);
                g.fillRect(0,
                           startHeight + (i * highFont.getHeight()) + spacing,
                           width, highFont.getHeight());
                g.setFont(highFont);
                g.setColor(highColor);
                g.drawString(mainMenu[i],
                             (width - highFont.stringWidth(mainMenu[i])) / 2,
                             startHeight + (i * highFont.getHeight()) + spacing,
                             20);
            } else {
                g.setFont(lowFont);
                g.setColor(lowColor);
                g.drawString(mainMenu[i],
                             (width - lowFont.stringWidth(mainMenu[i])) / 2,
                             startHeight + (i * highFont.getHeight()) + spacing,
                             20);
            }
        }
    }

    protected void keyPressed(int code) {
        if (getGameAction(code) == Canvas.UP && menuIdx - 1 >= 0) {
            menuIdx--;
        } else if (getGameAction(code) == Canvas.DOWN &&
                   menuIdx + 1 < mainMenu.length) {
            menuIdx++;
        } else if (getGameAction(code) == Canvas.FIRE) {

            switch (menuIdx) {
            case NEW_GAME:
                System.out.println("开始新游戏");
                break;
            case HIGH_SCORE:
                System.out.println("显示得分");
                break;
            case SETTINGS:
                System.out.println("设置");
                break;
            case HELP:
                System.out.println("帮助");
                break;
            case ABOUT:
                System.out.println("关于");
                break;
            }
        }
    }
}