DBMNG数据库管理与应用

书籍是全世界的营养品。生活里没有书籍,就好像没有阳光;智慧里没有书籍,就好像鸟儿没有翅膀。
当前位置:首页 > 移动应用 > JavaME

JavaMe低级界面绘图之菜单

【问题描述】

手机程序中,菜单使用很普遍,JavaMe中,如何绘制菜单呢?

下面分享一下实现的菜单功能,实现效果如图1所示:

【代码清单】

Color.java(借鉴 了kf156(亚日),J2ME canvas 多级菜单的实现)

  1. package com.token.view.components;  
  2.  
  3. /** 
  4.  * 颜色类 
  5.  *  
  6.  * @author kf156(亚日) 
  7.  *  
  8.  * @Email kf156@QQ.com 
  9.  *  
  10.  * @CopyrightNotice 此代码仅供学习交流,您可以使用并传播本源代码,但请保留作者信息。切勿商用,若有需要,请先于本人联系。 
  11.  */ 
  12. public class Color {  
  13.       
  14.     public static int strong = 0xFF8C00;//令牌显示颜色 
  15.       
  16.     public static int warning1 = 0xFF4500;// 警告颜色 
  17.       
  18.     public static int warning2 = 0xFF0000;// 警告颜色 
  19.       
  20.     public static int error = 0xFF0000;// 错误颜色 
  21.       
  22.     public static int background = 0xF0FAE6;// 背景颜色 
  23.  
  24.     public static int menuFrame = 0xB8E58A;// 窗体框架颜色 
  25.  
  26.     public static int label = 0xF0F8FF;// 标题颜色 
  27.  
  28.     public static int text = 0x314F12;// 正文颜色 
  29.  
  30.     public static int selectText = 0xFFFFFF;// 被选择后的正文颜色 
  31.  
  32.     public static int selectBg = 0x548720;// 被选择后的背景颜色 
  33.  
  34.     public static int cmdBarTop = 0x9FDB63;// 命令栏上半部分颜色 
  35.  
  36.     public static int cmdBarBottom = 0x81D130;// 命令栏下半部分颜色 
  37.  
  38.     public static int menuBg = 0xE0F5CB;// 命令菜单主背景色 
  39.  
  40.     public static int cmdSelectTop = 0x7DAB4F;// 命令菜单选中项上半部分颜色 
  41.  
  42.     public static int cmdSelectBottom = 0x5B9422;// 命令菜单选中项下半部分颜色 
  43.  
  44.     public static int linkClick = 0xFF0000;// 超链接点击后的颜色 
  45.       
  46.     public static int frame = 0x458B00;//边框颜色 
  47.       
  48.     public static int frameBg = 0x556B2F;//边框背景颜色 
  49.  
  50.     public static final int GREY = 0xBBBBBB;// 灰边框 
  51.  
  52.     public static final int BLACK = 0x0;// 黑色 
  53.       
  54.     public static final int WHITE = 0xFFFFFF;// 白色 
  55.  
  56.     public static final int TRANSLUCENCY_WRITE = 0x55FFFFFF;// 白色半透明 
  57.  
  58.     public static final int TRANSLUCENCY_BLACK = 0x55000000;// 黑色半透明 
  59.  
  60.     public static final byte STYLE_RED = 0;// 红色 
  61.  
  62.     public static final byte STYLE_ORANGE = 1;// 橙色 
  63.  
  64.     public static final byte STYLE_YELLOW = 2;// 黄色 
  65.  
  66.     public static final byte STYLE_GREEN = 3;// 绿色 
  67.  
  68.     public static final byte STYLE_LIME = 4;// 酸橙色 
  69.  
  70.     public static final byte STYLE_CYAN = 5;// 青色 
  71.  
  72.     public static final byte STYLE_AQUA = 6;// 浅绿色 
  73.  
  74.     public static final byte STYLE_SKY_BLUE = 7;// 天蓝色 
  75.  
  76.     public static final byte STYLE_BLUE = 8;// 蓝色 
  77.  
  78.     public static final byte STYLE_PURPLE = 9;// 紫色 
  79.  
  80.     public static final byte STYLE_FUCHSIA = 10;// 紫红色 
  81.  
  82.     public static final byte STYLE_MAGENTA = 11;// 红紫色 
  83.       
  84.     public static final byte STYLE_BLACK = 12;// 黑色 
  85.  
  86.     public static int colorName = STYLE_GREEN;// 风格名称 
  87.  
  88.     // 红色风格H0(#FF0000) 
  89.     private static void redColor() {  
  90.         background = 0xFAE7E7;// 背景颜色 
  91.         label = 0xFAEBD7;// 标题颜色 
  92.         text = 0x4F1212;// 正文颜色 
  93.         // selectTextColor = 0xFFFFFF;// 被选择后的正文颜色 
  94.         menuFrame = 0xE58A8A;// 窗体框架颜色 
  95.         menuBg = 0xF5CBCB;// 命令菜单主背景色 
  96.         selectBg = 0x872020;// 被选择后的背景颜色 
  97.         cmdBarTop = 0xDB6363;// 命令栏上半部分颜色 
  98.         cmdBarBottom = 0xD13030;// 命令栏下半部分颜色 
  99.         cmdSelectTop = 0xAB4F4F;// 命令菜单选中项上半部分颜色 
  100.         cmdSelectBottom = 0x942222;// 命令菜单选中项下半部分颜色 
  101.         frame = 0xF70909;//边框颜色 
  102.         frameBg = 0xD52B2B;//边框背景颜色 
  103.     }  
  104.  
  105.     // 橙色风格H30(#FF7F00) 
  106.     private static void orangeColor() {  
  107.         background = 0xFAF0E6;// 背景颜色 
  108.         label = 0xFFE4B5;// 标题颜色 
  109.         text = 0x4F3112;// 正文颜色 
  110.         // selectTextColor = 0xFFFFFF;// 被选择后的正文颜色 
  111.         menuFrame = 0xE5B88A;// 窗体框架颜色 
  112.         menuBg = 0xF5E0CB;// 命令菜单主背景色 
  113.         selectBg = 0x875420;// 被选择后的背景颜色 
  114.         cmdBarTop = 0xDB9F63;// 命令栏上半部分颜色 
  115.         cmdBarBottom = 0xD18130;// 命令栏下半部分颜色 
  116.         cmdSelectTop = 0xAB7D4F;// 命令菜单选中项上半部分颜色 
  117.         cmdSelectBottom = 0x945B22;// 命令菜单选中项下半部分颜色 
  118.         frame = 0xF76809;//边框颜色 
  119.         frameBg = 0xD56F2B;//边框背景颜色 
  120.     }  
  121.  
  122.     // 黄色风格H60(#FFFF00) 
  123.     private static void yellowColor() {  
  124.         background = 0xFAFAE6;// 背景颜色 
  125.         label = 0xFFFACD;// 标题颜色 
  126.         text = 0x4F4F12;// 正文颜色 
  127.         // selectText = 0xFFFF00;// 被选择后的正文颜色 
  128.         menuFrame = 0xE5E58A;// 窗体框架颜色 
  129.         menuBg = 0xF5F5CB;// 命令菜单主背景色 
  130.         selectBg = 0x878720;// 被选择后的背景颜色 
  131.         cmdBarTop = 0xDBDB63;// 命令栏上半部分颜色 
  132.         cmdBarBottom = 0xD1D130;// 命令栏下半部分颜色 
  133.         cmdSelectTop = 0xABAB4F;// 命令菜单选中项上半部分颜色 
  134.         cmdSelectBottom = 0x949422;// 命令菜单选中项下半部分颜色 
  135.         frame = 0xF7C709;//边框颜色 
  136.         frameBg = 0xD5D52B;//边框背景颜色 
  137.     }  
  138.  
  139.     // 绿色风格H90(#80FF00) 
  140.     private static void greenColor() {  
  141.         background = 0xF0FAE6;// 背景颜色 
  142.         label = 0xF0F8FF;// 标题颜色 
  143.         text = 0x314F12;// 正文颜色 
  144.         // selectTextColor = 0xFFFFFF;// 被选择后的正文颜色 
  145.         menuFrame = 0xB8E58A;// 窗体框架颜色 
  146.         menuBg = 0xE0F5CB;// 命令菜单主背景色 
  147.         selectBg = 0x548720;// 被选择后的背景颜色 
  148.         cmdBarTop = 0x9FDB63;// 命令栏上半部分颜色 
  149.         cmdBarBottom = 0x81D130;// 命令栏下半部分颜色 
  150.         cmdSelectTop = 0x7DAB4F;// 命令菜单选中项上半部分颜色 
  151.         cmdSelectBottom = 0x5B9422;// 命令菜单选中项下半部分颜色 
  152.         frame = 0x458B00;//边框颜色 
  153.         frameBg = 0x556B2F;//边框背景颜色 
  154.     }  
  155.  
  156.     // 酸橙色风格H120(0x00FF00) 
  157.     private static void limeColor() {  
  158.         background = 0xE6FAE6;// 背景颜色 
  159.         label = 0xEEE9BF;// 标题颜色 
  160.         text = 0x124F12;// 正文颜色 
  161.         // selectTextColor = 0xFFFFFF;// 被选择后的正文颜色 
  162.         menuFrame = 0x8AE58A;// 窗体框架颜色 
  163.         menuBg = 0xCBF5CB;// 命令菜单主背景色 
  164.         selectBg = 0x208720;// 被选择后的背景颜色 
  165.         cmdBarTop = 0x63DB63;// 命令栏上半部分颜色 
  166.         cmdBarBottom = 0x30D130;// 命令栏下半部分颜色 
  167.         cmdSelectTop = 0x4FAB4F;// 命令菜单选中项上半部分颜色 
  168.         cmdSelectBottom = 0x229422;// 命令菜单选中项下半部分颜色 
  169.         frame = 0x09F738;//边框颜色 
  170.         frameBg = 0x2BD54D;//边框背景颜色 
  171.     }  
  172.  
  173.     // 青色风格H150(0x00FF80) 
  174.     private static void cyanColor() {  
  175.         background = 0xE6FAF0;// 背景颜色 
  176.         label = 0xE0FFFF;// 标题颜色 
  177.         text = 0x124F31;// 正文颜色 
  178.         // selectTextColor = 0xFFFFFF;// 被选择后的正文颜色 
  179.         menuFrame = 0x8AE5B8;// 窗体框架颜色 
  180.         menuBg = 0xCBF5E0;// 命令菜单主背景色 
  181.         selectBg = 0x208754;// 被选择后的背景颜色 
  182.         cmdBarTop = 0x63DB9F;// 命令栏上半部分颜色 
  183.         cmdBarBottom = 0x30D181;// 命令栏下半部分颜色 
  184.         cmdSelectTop = 0x4FAB7D;// 命令菜单选中项上半部分颜色 
  185.         cmdSelectBottom = 0x22945B;// 命令菜单选中项下半部分颜色 
  186.         frame = 0x4DB34D;//边框颜色 
  187.         frameBg = 0x8AB34D;//边框背景颜色 
  188.     }  
  189.  
  190.     // 浅绿色风格H180(#00FFFF) 
  191.     private static void aquaColor() {  
  192.         background = 0xE6FAFA;// 背景颜色 
  193.         label = 0xE0EEE0;// 标题颜色 
  194.         text = 0x445500;// 正文颜色 
  195.         // selectTextColor = 0xFFFFFF;// 被选择后的正文颜色 
  196.         menuFrame = 0x88AA55;// 窗体框架颜色 
  197.         menuBg = 0xE6FAFA;// 命令菜单主背景色 
  198.         selectBg = 0xAECC33;// 被选择后的背景颜色 
  199.         cmdBarTop = 0xBDE61A;// 命令栏上半部分颜色 
  200.         cmdBarBottom = 0xB8DD22;// 命令栏下半部分颜色 
  201.         cmdSelectTop = 0xB8DD22;// 命令菜单选中项上半部分颜色 
  202.         cmdSelectBottom = 0xB3D52B;// 命令菜单选中项下半部分颜色 
  203.         frame = 0x8E8E38;//边框颜色 
  204.         frameBg = 0x8B8B00;//边框背景颜色 
  205.     }  
  206.  
  207.     // 天蓝色风格H210(#0080FF) 
  208.     private static void skyBlueColor() {  
  209.         background = 0xE6F0FA;// 背景颜色 
  210.         label = 0xE0FFFF;// 标题颜色 
  211.         text = 0x12314F;// 正文颜色 
  212.         // selectTextColor = 0xFFFFFF;// 被选择后的正文颜色 
  213.         menuFrame = 0x8AB8E5;// 窗体框架颜色 
  214.         menuBg = 0xCBE0F5;// 命令菜单主背景色 
  215.         selectBg = 0x205487;// 被选择后的背景颜色 
  216.         cmdBarTop = 0x639FDB;// 命令栏上半部分颜色 
  217.         cmdBarBottom = 0x3081D1;// 命令栏下半部分颜色     
  218.         cmdSelectTop = 0x4F7DAB;// 命令菜单选中项上半部分颜色 
  219.         cmdSelectBottom = 0x225B94;// 命令菜单选中项下半部分颜色 
  220.         frame = 0x0997F7;//边框颜色 
  221.         frameBg = 0x2BB3D5;//边框背景颜色 
  222.     }  
  223.  
  224.     // 深蓝色风格H240(#0000FF) 
  225.     private static void blueColor() {  
  226.         background = 0xE6E6FA;// 背景颜色 
  227.         label = 0xE0EEEE;// 标题颜色 
  228.         text = 0x12124F;// 正文颜色 
  229.         // selectTextColor = 0xFFFFFF;// 被选择后的正文颜色 
  230.         menuFrame = 0x8A8AE5;// 窗体框架颜色 
  231.         menuBg = 0xCBCBF5;// 命令菜单主背景色 
  232.         selectBg = 0x202087;// 被选择后的背景颜色 
  233.         cmdBarTop = 0x6363DB;// 命令栏上半部分颜色 
  234.         cmdBarBottom = 0x3030D1;// 命令栏下半部分颜色 
  235.         cmdSelectTop = 0x4F4FAB;// 命令菜单选中项上半部分颜色 
  236.         cmdSelectBottom = 0x222294;// 命令菜单选中项下半部分颜色 
  237.         frame = 0x0909F7;//边框颜色 
  238.         frameBg = 0x2B2BD5;//边框背景颜色 
  239.     }  
  240.  
  241.     // 紫色风格H270(#8000FF) 
  242.     private static void purpleColor() {  
  243.         background = 0xF0E6FA;// 背景颜色 
  244.         label = 0xFDF5E6;// 标题颜色 
  245.         text = 0x31124F;// 正文颜色 
  246.         // selectTextColor = 0xFFFFFF;// 被选择后的正文颜色 
  247.         menuFrame = 0xB88AE5;// 窗体框架颜色 
  248.         menuBg = 0xE0CBF5;// 命令菜单主背景色 
  249.         selectBg = 0x542087;// 被选择后的背景颜色 
  250.         cmdBarTop = 0x9F63DB;// 命令栏上半部分颜色 
  251.         cmdBarBottom = 0x8130D1;// 命令栏下半部分颜色 
  252.         cmdSelectTop = 0x7D4FAB;// 命令菜单选中项上半部分颜色 
  253.         cmdSelectBottom = 0x5B2294;// 命令菜单选中项下半部分颜色 
  254.         frame = 0x6809F7;//边框颜色 
  255.         frameBg = 0x6F2BD5;//边框背景颜色 
  256.     }  
  257.  
  258.     // 紫红色风格H300(#FF00FF) 
  259.     private static void fuchsiaColor() {  
  260.         background = 0xFAE6FA;// 背景颜色 
  261.         label = 0xFAF0E6;// 标题颜色 
  262.         text = 0x4F124F;// 正文颜色 
  263.         // selectTextColor = 0xFFFFFF;// 被选择后的正文颜色 
  264.         menuFrame = 0xE58AE5;// 窗体框架颜色 
  265.         menuBg = 0xF5CBF5;// 命令菜单主背景色 
  266.         selectBg = 0x872087;// 被选择后的背景颜色 
  267.         cmdBarTop = 0xDB63DB;// 命令栏上半部分颜色 
  268.         cmdBarBottom = 0xD130D1;// 命令栏下半部分颜色     
  269.         cmdSelectTop = 0xAB4FAB;// 命令菜单选中项上半部分颜色 
  270.         cmdSelectBottom = 0x942294;// 命令菜单选中项下半部分颜色 
  271.         frame = 0x9709F7;//边框颜色 
  272.         frameBg = 0xB32BD5;//边框背景颜色 
  273.     }  
  274.  
  275.     // 红紫色风格H330(#FF007F) 
  276.     private static void magentaColor() {  
  277.         background = 0xFAE6F0;// 背景颜色 
  278.         label = 0xFFEFD5;// 标题颜色 
  279.         text = 0x4F1231;// 正文颜色 
  280.         // selectTextColor = 0xFFFFFF;// 被选择后的正文颜色 
  281.         menuFrame = 0xE58AB8;// 窗体框架颜色 
  282.         menuBg = 0xF5CBE0;// 命令菜单主背景色 
  283.         selectBg = 0x872054;// 被选择后的背景颜色 
  284.         cmdBarTop = 0xDB639F;// 命令栏上半部分颜色 
  285.         cmdBarBottom = 0xD13081;// 命令栏下半部分颜色         
  286.         cmdSelectTop = 0xAB4F7D;// 命令菜单选中项上半部分颜色 
  287.         cmdSelectBottom = 0x94225B;// 命令菜单选中项下半部分颜色 
  288.         frame = 0xC709F7;//边框颜色 
  289.         frameBg = 0xD52BD5;//边框背景颜色 
  290.     }  
  291.       
  292.     //黑色风格(#000000) 
  293.     private static void blackColor() {  
  294.         background = 0xEDEDED;// 背景颜色 
  295.         label = 0xEAEAEA;// 标题颜色 
  296.         text = 0x030303;// 正文颜色 
  297.         // selectTextColor = 0xFFFFFF;// 被选择后的正文颜色 
  298.         menuFrame = 0x2E2E2E;// 窗体框架颜色 
  299.         menuBg = 0xC9C9C9;// 命令菜单主背景色 
  300.         selectBg = 0x4A4A4A;// 被选择后的背景颜色 
  301.         cmdBarTop = 0x3B3B3B;// 命令栏上半部分颜色 
  302.         cmdBarBottom = 0x383838;// 命令栏下半部分颜色 
  303.         cmdSelectTop = 0x2E2E2E;// 命令菜单选中项上半部分颜色 
  304.         cmdSelectBottom = 0x0D0D0D;// 命令菜单选中项下半部分颜色 
  305.         frame = 0x0A0A0A;//边框颜色 
  306.         frameBg = 0x636363;//边框背景颜色 
  307.     }  
  308.  
  309.     // 风格切换 
  310.     public static void nextColor() {  
  311.         set(++colorName);  
  312.  
  313.     }  
  314.  
  315.     /** 
  316.      * 设置颜色 
  317.      *  
  318.      */ 
  319.     public static void set(int color) {  
  320.         // System.out.println(color); 
  321.         switch (color) {  
  322.         case STYLE_RED:// 红色 
  323.             redColor();  
  324.             break;  
  325.  
  326.         case STYLE_ORANGE:// 橙色 
  327.             orangeColor();  
  328.             break;  
  329.  
  330.         case STYLE_YELLOW:// 黄色 
  331.             yellowColor();  
  332.             break;  
  333.  
  334.         case STYLE_GREEN:// 绿色 
  335.             greenColor();  
  336.             break;  
  337.  
  338.         case STYLE_LIME:// 酸橙色 
  339.             limeColor();  
  340.             break;  
  341.  
  342.         case STYLE_CYAN:// 青色 
  343.             cyanColor();  
  344.             break;  
  345.  
  346.         case STYLE_AQUA:// 浅绿色 
  347.             aquaColor();  
  348.             break;  
  349.  
  350.         case STYLE_SKY_BLUE:// 天蓝色 
  351.             skyBlueColor();  
  352.             break;  
  353.  
  354.         case STYLE_BLUE:// 蓝色 
  355.             blueColor();  
  356.             break;  
  357.  
  358.         case STYLE_PURPLE:// 紫色 
  359.             purpleColor();  
  360.             break;  
  361.  
  362.         case STYLE_FUCHSIA:// 紫红色 
  363.             fuchsiaColor();  
  364.             break;  
  365.  
  366.         case STYLE_MAGENTA:// 红紫色 
  367.             magentaColor();  
  368.             break;  
  369.               
  370.         case STYLE_BLACK:// 红紫色 
  371.             blackColor();  
  372.             break;  
  373.  
  374.         default:// 颜色循环完毕,返回红色 
  375.             colorName = STYLE_RED;  
  376.             redColor();// RED 
  377.             break;  
  378.         }  
  379.     }  
  380.  
  381.     // 设置新风格 
  382.     public static void setNewColor(int newColor) {  
  383.         background = newColor;  
  384.         menuFrame = background - 0x3A155D;  
  385.         label = background - 0x25A8E5;  
  386.         text = background - 0xC0ACD5;  
  387.         selectText = background + 0x100518;  
  388.         selectBg = background - 0x9D72C7;  
  389.         cmdBarTop = background - 0x531E84;  
  390.         cmdBarBottom = background - 0x742AB8;  
  391.     }  
  392. }  
  393.  


KeyID.java

  1. package com.token.view.components;  
  2.  
  3. public class KeyID {  
  4.     public static final byte KEY_EDIT=-5;//选择 
  5.     public static final byte SOFT_LEFT=-6;//左软键 
  6.     public static final byte SOFT_RIGHT=-7;//右软键 
  7.       
  8.     private KeyID(){  
  9.     }  
  10. }  

Menu.java

  1. package com.token.view.components;  
  2.  
  3. import javax.microedition.lcdui.*;  
  4. import javax.microedition.lcdui.game.*;  
  5. public class Menu   
  6. {  
  7.      private String leftOption;  
  8.      private String rightOption;  
  9.      private String cancelOption = "取消";  
  10.      private String okOption = "确定";  
  11.      private String[] menuOptions;  
  12.      private int padding;  
  13.      private Font ft;  
  14.      private int width;  
  15.      private int height;  
  16.      public int menuHeight;  
  17.        
  18.      public Menu(GameCanvas canvas)  
  19.      {  
  20.          width = canvas.getWidth();  
  21.          height = canvas.getHeight();  
  22.      }  
  23.        
  24.      public Menu(GameCanvas canvas, String leftOption, String rightOption, String[] menuOptions) {  
  25.           this.leftOption = leftOption;  
  26.           this.rightOption = rightOption;  
  27.           this.menuOptions = menuOptions;  
  28.               
  29.           width = canvas.getWidth();  
  30.           height = canvas.getHeight();  
  31.      } // end constructor 
  32.  
  33. //绘制软键选项 
  34.     public void drawMenu(GameCanvas canvas, Graphics graphics, String leftOption, String rightOption)  
  35.     {  
  36.         width = canvas.getWidth();  
  37.         height = canvas.getHeight();  
  38.            
  39.         padding = 5;  
  40.         ft = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD,  
  41.                 Font.SIZE_LARGE);  
  42.         int fontHeight = ft.getHeight();  
  43.         menuHeight = fontHeight + 2 * padding;  
  44.           
  45.         graphics.setColor(Color.cmdSelectTop);  
  46.         graphics.fillRect(0, height-menuHeight, width, menuHeight/2+1);  
  47.         graphics.setColor(Color.cmdSelectBottom);  
  48.         graphics.fillRect(0, height-menuHeight/2, width, menuHeight/2);  
  49.           
  50.         graphics.setFont(ft);  
  51.         graphics.setColor(Color.label);  
  52.           
  53.         if(leftOption!=null)  
  54.         {  
  55.             graphics.drawString(leftOption, padding, height-padding, Graphics.LEFT | Graphics.BOTTOM);  
  56.         }  
  57.           
  58.         if(rightOption!=null)  
  59.         {  
  60.             graphics.drawString(rightOption, width-padding, height-padding, Graphics.RIGHT | Graphics.BOTTOM);  
  61.         }  
  62.     }  
  63.    
  64.     public void drawSubMenu(Graphics graphics, int selectedOptionIndex)  
  65.     {  
  66.         padding = 6;  
  67.         ft = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD,  
  68.                 Font.SIZE_MEDIUM);  
  69.          int fontHeight = ft.getHeight();  
  70.            
  71.         //确定菜单的最大宽度、最大高度 
  72.          int submenuMaxWidth = 0;  
  73.          int submenuMaxHeight = 0;  
  74.          int currentWidth = 0;  
  75.            
  76.          for (int i = 0; i < menuOptions.length; i++)   
  77.          {  
  78.             currentWidth = ft.stringWidth(menuOptions[i]);  
  79.             if (currentWidth > submenuMaxWidth)   
  80.             {  
  81.                 submenuMaxWidth = currentWidth;  
  82.             }  
  83.             submenuMaxHeight += fontHeight + padding;  
  84.          }  
  85.          submenuMaxWidth += 6 * padding;//5个padding用于显示字符 
  86.            
  87.          int sub_padding = 3;  
  88.          int sub_margin_left = 2;  
  89.          int sub_margin_bottom = 2;  
  90.            
  91.          int submenuFrameMaxWidth = submenuMaxWidth + 2*sub_padding;  
  92.          int submenuFrameMaxHeight = submenuMaxHeight + 2*sub_padding;  
  93.  
  94.          graphics.setColor(Color.menuFrame);  
  95.          graphics.drawRoundRect(sub_margin_left, // x 
  96.                  height - menuHeight - submenuFrameMaxHeight-sub_margin_bottom, // y 
  97.                  submenuFrameMaxWidth, submenuFrameMaxHeight,5,5);  
  98.          graphics.setColor(Color.menuBg);  
  99.          graphics.fillRoundRect(sub_margin_left+1, // x 
  100.                  height - menuHeight - submenuFrameMaxHeight-sub_margin_bottom+1, // y 
  101.                  submenuFrameMaxWidth-1, submenuFrameMaxHeight-1,5,5);  
  102.  
  103.            
  104.          graphics.setFont(ft);  
  105.          int menuOptionX = sub_margin_left + sub_padding + padding;  
  106.          int menuOptionY = height - menuHeight - submenuMaxHeight-sub_margin_bottom-sub_padding+padding/2;  
  107.            
  108.          for (int i = 0; i < menuOptions.length; i++)   
  109.          {  
  110.              if (i != selectedOptionIndex)   
  111.              {  
  112.                  // draw unselected menu option 
  113.                  graphics.setColor(Color.text); // black 
  114.                  graphics.drawString(menuOptions[i], menuOptionX, menuOptionY, Graphics.LEFT  
  115.                    | Graphics.TOP);  
  116.              }  
  117.              else   
  118.              {  
  119.                  // draw selected menu option 
  120.                  graphics.setColor(Color.selectBg);  
  121.                  int highOptionY=height - menuHeight - sub_margin_bottom - submenuMaxHeight + i*(fontHeight + padding)-sub_padding;  
  122.                  graphics.fillRect(sub_margin_left+sub_padding,highOptionY,submenuMaxWidth+1,fontHeight + padding);  
  123.                  graphics.setColor(Color.selectText);  
  124.                  graphics.drawString(menuOptions[i], menuOptionX, menuOptionY, Graphics.LEFT  
  125.                    | Graphics.TOP);  
  126.              }   
  127.              menuOptionY += padding + fontHeight;  
  128.          }  
  129.     }  
  130.       
  131.     public void drawInactiveMenu(GameCanvas canvas, Graphics graphics)   
  132.     {  
  133.       drawMenu(canvas, graphics,leftOption,rightOption);  
  134.       canvas.flushGraphics();  
  135.     } // end drawInactiveMenu 
  136.       
  137.      public void drawActiveMenu(GameCanvas canvas, Graphics graphics, int selectedOptionIndex)   
  138.      {  
  139.          drawMenu(canvas, graphics,cancelOption,okOption);  
  140.          // draw menu options 
  141.          if (menuOptions != null)   
  142.          {  
  143.              drawSubMenu(graphics,selectedOptionIndex);  
  144.              canvas.flushGraphics();  
  145.         }  
  146.      }  

MainScreen.java(程序有删减,仅供学习交流)

  1. package com.token.view;  
  2.  
  3. import javax.microedition.lcdui.*;  
  4. import javax.microedition.lcdui.game.GameCanvas;  
  5.  
  6. import com.token.util.*;  
  7. import com.token.view.components.*;  
  8.  
  9. public class MainScreen extends GameCanvas   
  10. {  
  11.  
  12.     private UIController controller;  
  13.     private Font ft;  
  14.     private int width;  
  15.     private int height;  
  16.       
  17.     static final String[] menuOptions = { "1.查看","2.更新","3.用户管理","4.帮助","5.换肤","6.退出" };   
  18.       
  19.     static int menuIdx;// 记录是在第几个菜单处按下的确定键。    
  20.     private int currentlySelectedIndex = 0;     
  21.     private boolean menuIsActive = false;     
  22.     private String leftOption;     
  23.     private String rightOption;     
  24.     private Graphics graphics;     
  25.     private Menu menu;  
  26.       
  27.     public MainScreen(UIController control)  
  28.     {  
  29.         super(false);  
  30.         this.controller = control;  
  31.         setFullScreenMode(true);  
  32.           
  33.         ft = Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_BOLD,Font.SIZE_LARGE);  
  34.           
  35.         width = getWidth();  
  36.         height = getHeight();  
  37.           
  38.         graphics = getGraphics();  
  39.         menuIdx = 0;  
  40.           
  41.         leftOption = "选项";     
  42.         rightOption = "";     
  43.           
  44.         menu = new Menu(this, leftOption, rightOption, menuOptions);  
  45.     }  
  46.       
  47.     public void show(Object[] args) {     
  48.           
  49.         draw();  
  50.         //flushGraphics(); 
  51.     }   
  52.       
  53.     private void draw()  
  54.     {  
  55.         menu.drawInactiveMenu(this, graphics);  
  56.     }  
  57.       
  58.     public void drawBase()  
  59.     {  
  60.         //clearScreen(); 
  61.     }  
  62.       
  63.  
  64.     public void clearScreen()  
  65.     {  
  66.         graphics.setColor(0xff,0xff,0xff);  
  67.         graphics.fillRect(0, 0, width, height);  
  68.         flushGraphics();  
  69.     }  
  70.  
  71.     protected void keyPressed(int keyCode)   
  72.     {  
  73.           if (menuIsActive)  
  74.           {  
  75.                if (keyCode == KeyID.SOFT_LEFT)   
  76.                {  
  77.                 // cancel pressed, draw inactive menu again 
  78.                 drawBase();  
  79.                 menu.drawInactiveMenu(this, graphics);  
  80.                 menuIsActive = false;  
  81.                }  
  82.                  
  83.                switch(keyCode)  
  84.                {  
  85.                    case KEY_NUM1:  
  86.                        currentlySelectedIndex = 0;  
  87.                        menu.drawActiveMenu(this, graphics, currentlySelectedIndex);  
  88.                        break;  
  89.                    case KEY_NUM2:  
  90.                        currentlySelectedIndex = 1;  
  91.                        menu.drawActiveMenu(this, graphics, currentlySelectedIndex);  
  92.                        break;  
  93.                    case KEY_NUM3:  
  94.                        currentlySelectedIndex = 2;  
  95.                        menu.drawActiveMenu(this, graphics, currentlySelectedIndex);  
  96.                        break;  
  97.                    case KEY_NUM4:  
  98.                        currentlySelectedIndex = 3;  
  99.                        menu.drawActiveMenu(this, graphics, currentlySelectedIndex);  
  100.                        break;  
  101.                  case KEY_NUM5:  
  102.                        currentlySelectedIndex = 4;  
  103.                        menu.drawActiveMenu(this, graphics, currentlySelectedIndex);  
  104.                        break;  
  105.                 case KEY_NUM6:  
  106.                        currentlySelectedIndex = 5;  
  107.                        menu.drawActiveMenu(this, graphics, currentlySelectedIndex);  
  108.                        break;  
  109.                 case KeyID.SOFT_RIGHT:  
  110.                        keyCode = FIRE;  
  111.                        break;  
  112.                    default:;  
  113.                }  
  114.                
  115.                keyCode = (keyCode != FIRE)?getGameAction(keyCode):keyCode;  
  116.                  
  117.                switch(keyCode)  
  118.                {  
  119.                    case UP:   
  120.                         if(currentlySelectedIndex==0)  
  121.                         {  
  122.                             currentlySelectedIndex = menuOptions.length-1;  
  123.                         }  
  124.                         else 
  125.                         {  
  126.                             currentlySelectedIndex--;  
  127.                             if (currentlySelectedIndex < 0)   
  128.                             {  
  129.                              currentlySelectedIndex = 0; // stay within limits 
  130.                             }   
  131.                         }  
  132.                         menu.drawActiveMenu(this, graphics, currentlySelectedIndex);  
  133.                          
  134.                         break;  
  135.                    case DOWN:  
  136.                        if(currentlySelectedIndex==menuOptions.length-1)  
  137.                        {  
  138.                          currentlySelectedIndex = 0;  
  139.                        }  
  140.                        else 
  141.                        {  
  142.                            currentlySelectedIndex++;  
  143.                            if (currentlySelectedIndex >= menuOptions.length)  
  144.                            {  
  145.                                currentlySelectedIndex = menuOptions.length - 1;  
  146.                            }  
  147.                        }  
  148.                        menu.drawActiveMenu(this, graphics, currentlySelectedIndex);  
  149.                        break;  
  150.                    case FIRE:   
  151.                    {  
  152.                        drawBase();  
  153.                        graphics.setColor(0x000000);  
  154.                        menu.drawInactiveMenu(this, graphics);  
  155.                        menuIsActive = false;  
  156.                        //按键处理 
  157.                        switch(currentlySelectedIndex)  
  158.                        {  
  159.                            case 0:  
  160.                            {  
  161.                                //Object []args = {"viewScreen", "userAuth_name",null,null}; 
  162.                                //controller.handleEvent(UIController.EventID.EVENT_VIEW_AUTH,args); 
  163.                                break;  
  164.                            }  
  165.                            case 1:  
  166.                            {  
  167.                                //Object []args = {"updateScreen", "userAuth_name",null,null}; 
  168.                                //controller.handleEvent(UIController.EventID.EVENT_UPDATE_AUTH, args); 
  169.                                break;  
  170.                            }  
  171.                            case 2:  
  172.                            {  
  173.                                //Object []args = {"manageScreen", "userAuth_name",null,null}; 
  174.                                //controller.handleEvent(UIController.EventID.EVENT_USER_MANAGE_AUTH, args); 
  175.                                break;  
  176.                            }  
  177.                            case 3:  
  178.                            {  
  179.                               //controller.handleEvent(UIController.EventID.EVENT_SHOW_HELP,null); 
  180.                                break;  
  181.                            }  
  182.                            case 4:  
  183.                            {  
  184.                                break;  
  185.                            }  
  186.                            case 5:  
  187.                            {  
  188.                                //controller.handleEvent(UIController.EventID.EVENT_EXIT,null); 
  189.                                break;  
  190.                            }  
  191.                            default:;  
  192.                        }  
  193.                        break;  
  194.                    }  
  195.                    default:;  
  196.                }  
  197.           }  
  198.           else   
  199.           {  
  200.               if (keyCode == KeyID.SOFT_LEFT)   
  201.               { // "Options" pressed 
  202.                   menu.drawActiveMenu(this, graphics, currentlySelectedIndex);  
  203.                   menuIsActive = true;  
  204.               }  
  205.               else if (keyCode == KeyID.SOFT_RIGHT)   
  206.               {  
  207.  
  208.               }  
  209.           }  
  210.      }  
  211. }  

*关于UIController,后续文章将给予解释。 

【参考文献】 

(1) J2ME canvas 多级菜单的实现

(2) http://www.iteye.com/topic/479217

原文链接:http://blog.csdn.net/tandesir/article/details/7536778

本站文章内容,部分来自于互联网,若侵犯了您的权益,请致邮件chuanghui423#sohu.com(请将#换为@)联系,我们会尽快核实后删除。
Copyright © 2006-2023 DBMNG.COM All Rights Reserved. Powered by DEVSOARTECH            豫ICP备11002312号-2

豫公网安备 41010502002439号