看了<<J2me中使用Canvas创建高级菜单>>后,将canvas创建高级菜单归结为两个问题:菜单的绘制和菜单事件的处理.设计了三个类:MenuMidlet,MenuScreen,Menu。
MenuMidlet是主类,该类继承了MIDlet类;
MenuScreen类负责事件处理;
Menu类负责菜单的绘制
2.菜单绘制
菜单有两种状态:激活状态和非激活状态.
在激活状态下,显示菜单项,并且可以接受用户的上下选择事件
在非激活状态下,隐藏菜单项,不接受用户的上下选择事件
菜单的绘制包括激活状态下的菜单绘制和非激活状态下的菜单绘制
非激活状态下的菜单绘制包括:左菜单(options)和右菜单(exit)绘制
激活状态下的菜单绘制包括:左菜单(options)和右菜单(cancel)绘制,左菜单项(新建,设定,记录,帮助,关于)绘制。如图:
 
3.菜单事件处理
Canvas提供了keyPressed(int key)来响应用户的按键 。
Canvas中的gameAction是将手机键盘映射成为游戏动作的机制。
需要响应UP,DOWN,FIRE,左菜单键,右菜单键事件
以下是源代码,在WTK2.5.2下编译通过
1)MenuMidlet类
Java代码
import javax.microedition.midlet.MIDlet; 
import javax.microedition.midlet.MIDletStateChangeException; 
import javax.microedition.lcdui.*; 
public class MenuMidlet extends MIDlet  { 
    private   MenuScreen menuScreen; 
    Display display; 
 public MenuMidlet() { 
  display= Display.getDisplay(this); 
  menuScreen = new MenuScreen(this); 
 } 
 
 protected void startApp() throws MIDletStateChangeException { 
 } 
 
 protected void pauseApp() { 
 } 
 
 protected void destroyApp(boolean unconditional) 
   throws MIDletStateChangeException { 
 } 

2)MenuScreen类
Java代码
import javax.microedition.lcdui.*; 
import javax.microedition.lcdui.game.GameCanvas; 
import javax.microedition.media.*; 
public class MenuScreen  extends GameCanvas { 
 static int width; // screen width 
 static int height; // screen height 
 private int LEFT_SOFTKEY_CODE = -6; 
 private int RIGHT_SOFTKEY_CODE = -7; 
 // Menu Item Labels 
 static final String[] menuOptions = { "新建", "设置", 
   "记录", "帮助", "关于" }; 
 static int menuIdx;// 记录是在第几个菜单处按下的确定键。 
 private int currentlySelectedIndex = 0; 
 private boolean menuIsActive = false; 
 private MenuMidlet midlet; 
 private String leftOption; 
 private String rightOption; 
 private Graphics g; 
 private Menu menu; 
 
 // Constructor 
 public MenuScreen(MenuMidlet midlet) { 
  super(false); 
  this.midlet = midlet; 
  // Get Width and Height of Canvas 
  width = getWidth(); 
  height = getHeight(); 
  setFullScreenMode(true); 
  menuIdx = 0; 
  g = getGraphics(); 
  leftOption = "Options"; 
  rightOption = "Exit"; 
  menu = new Menu(leftOption, rightOption, menuOptions); 
  start(); 
  midlet.display.setCurrent(this); 
 } 
 
 public void start() { 
  clearScreen(); 
  menu.drawInactiveMenu(this, g); 
 } 
 
 public void clearScreen() { 
  g.setColor(0xffffff); // white 
  g.fillRect(0, 0, width, height); 
  flushGraphics(); 
 } 
  
 protected void keyPressed(int keyCode) { 
  if (menuIsActive) { 
   if (keyCode == RIGHT_SOFTKEY_CODE) { 
    // draw inactive menu again 
    clearScreen(); 
    menu.drawInactiveMenu(this, g); 
    menuIsActive = false; 
   } 
   keyCode = getGameAction(keyCode); 
   if (keyCode == UP) { 
    currentlySelectedIndex--; 
    if (currentlySelectedIndex < 0) { 
     currentlySelectedIndex = 0; // stay within limits 
    } 
    menu.drawActiveMenu(this, g, currentlySelectedIndex); 
   } 
   else if (keyCode == DOWN) { 
    currentlySelectedIndex++; 
    if (currentlySelectedIndex >= menuOptions.length) { 
     currentlySelectedIndex = menuOptions.length - 1; 
    } 
    menu.drawActiveMenu(this, g, currentlySelectedIndex); 
   } 
   else if (keyCode == FIRE) { 
    clearScreen(); 
    g.setColor(0x000000); 
    g.drawString("[" + menuOptions[currentlySelectedIndex] 
      + "] was selected", 10, 15, g.LEFT | g.TOP); 
    menu.drawInactiveMenu(this, g); 
    menuIsActive = false; 
   } 
  } 
  else { 
   if (keyCode == LEFT_SOFTKEY_CODE) { // "Options" pressed 
    menu.drawActiveMenu(this, g, currentlySelectedIndex); 
    menuIsActive = true; 
   } 
   else if (keyCode == RIGHT_SOFTKEY_CODE) { 
     
   } 
  } 
 } 

3)Menu类
Java代码
import java.util.*; 
import javax.microedition.lcdui.*; 
import javax.microedition.lcdui.game.*; 
public class Menu { 
 private String leftOption; 
 private String rightOption; 
 private String cancelOption = "Cancel"; 
 private String[] menuOptions; 
 private int padding = 5; 
 //颜色设置 
 private static final int defaultFontColor = 0x000000; // 默认字体颜色 
 private static final int selectedFontColor=0xfff000;  //选中后字体颜色 
 private static final int bgColor = 0xCCCCCC;// 菜单背景色 
 private static final int highLight=0x0000ff;//选中的菜单高亮显示颜色 
  
 public Menu(String leftOption, String rightOption, String[] menuOptions) { 
  this.leftOption = leftOption; 
  this.rightOption = rightOption; 
  this.menuOptions = menuOptions; 
 } // end constructor 
 
 public void drawInactiveMenu(GameCanvas canvas, Graphics g) { 
  // create inactive menu font 
  Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, 
    Font.SIZE_MEDIUM); 
  int fontHeight = font.getHeight(); 
  // clear inactive menu background 
  int width = canvas.getWidth(); 
  int height = canvas.getHeight(); 
  g.setColor(bgColor); // grey color 
  g.fillRect(0, height - fontHeight - 2 * padding, width, height); 
  // draw left and right menu options 
  g.setFont(font); 
  g.setColor(defaultFontColor); // black 
  g.drawString(leftOption, padding, height - padding, g.LEFT | g.BOTTOM); 
  g.drawString(rightOption, width - padding, height - padding, g.RIGHT 
    | g.BOTTOM); 
  canvas.flushGraphics(); 
 } // end drawInactiveMenu 
 
 public void drawActiveMenu(GameCanvas canvas, Graphics g, 
   int selectedOptionIndex) { 
  Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, 
    Font.SIZE_MEDIUM); 
  int fontHeight = font.getHeight(); 
  int width = canvas.getWidth(); 
  int height = canvas.getHeight(); 
  g.setColor(bgColor); 
  g.fillRect(0, height - fontHeight - 2 * padding, width, height); 
  // draw default menu bar options 
  g.setFont(font); 
  g.setColor(0x000000); // black 
  g.drawString(leftOption, padding, height - padding, g.LEFT | g.BOTTOM); 
  // draw "Cancel" option 
  g.drawString(cancelOption, width - padding, height - padding, g.RIGHT 
    | g.BOTTOM); 
  canvas.flushGraphics(); 
  // draw menu options 
  if (menuOptions != null) { 
   //确定菜单的最大宽度、最大高度 
   int menuMaxWidth = 0; 
   int menuMaxHeight = 0; 
   int currentWidth = 0; 
   for (int i = 0; i < menuOptions.length; i++) { 
    currentWidth = font.stringWidth(menuOptions[i]); 
    if (currentWidth > menuMaxWidth) { 
     menuMaxWidth = currentWidth; 
    } 
    menuMaxHeight += fontHeight + padding; 
   } 
   menuMaxWidth += 6 * padding; 
   g.setColor(bgColor); 
   g.fillRect(0, // x 
     height - fontHeight - 2 * padding - menuMaxHeight, // y 
     menuMaxWidth, menuMaxHeight); 
 
 
   g.setFont(font); 
   int menuOptionX = padding; 
   int menuOptionY = height - fontHeight - 2 * padding - menuMaxHeight 
     + padding; 
    for(int i=0; i<=menuOptions.length; i++) { 
     g.setColor(highLight); 
     int lineOptionY= height-fontHeight - 2 * padding- menuMaxHeight+i*(fontHeight + padding); 
              g.drawLine(0, lineOptionY, 
                menuMaxWidth-1, lineOptionY); 
          } 
   for (int i = 0; i < menuOptions.length; i++) { 
    if (i != selectedOptionIndex) { 
     // draw unselected menu option 
     g.setColor(defaultFontColor); // black 
     g.drawString(menuOptions[i], menuOptionX, menuOptionY, g.LEFT 
       | g.TOP); 
    } 
    else { 
     // draw selected menu option 
     g.setColor(highLight); 
     int highOptionY=height - fontHeight - 2 * padding - menuMaxHeight+i*(fontHeight + padding); 
     g.fillRect(0,highOptionY,menuMaxWidth,fontHeight + padding); 
     g.setColor(selectedFontColor); 
     g.drawString(menuOptions[i], menuOptionX, menuOptionY, g.LEFT 
       | g.TOP); 
      
    }  
    menuOptionY += padding + fontHeight; 
   } 
   canvas.flushGraphics(); 
  } 
 }