⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 menugroup.java

📁 J2me类windows 菜单的实现,以及拼图游戏
💻 JAVA
字号:
//J2ME 中实现仿windows菜单
//菜单组,用来把菜单项添加到菜单组中,并把整个菜单组在画板中显示出来
//翁荣建
//2005-06-10
//MenuGroup.java
package menu;
import javax.microedition.lcdui.*;
public class MenuGroup
{
  Menu[] menu;//menu数组
  private int left;
  private int top;
  private int topbak;//用来还原top
  private int width;
  private int count         = 0;//菜单组中所add菜单项的个数
  private int length        = 0;//菜单组元数,count的最大值
  private int selectIndex   = 0;//菜单组中被选中的菜单项
  private boolean isfocus   = false;//是否在焦点上
  private boolean isVisable = true;//是否可视
  private Font font;
  private Font fonttmp;
  //public MenuGroup()
  //{
  //}
  public MenuGroup(int left,int top,int width,int length)
  {//构造菜单组
   this.left   = left;
   this.top    = top;
   this.topbak = top;
   this.width  = width;
   this.length = length;
   menu=new Menu[length];
  }
  public void Add(Menu menu)
  { //添菜单项 参数为菜单项
    if(count < length){
    this.menu[count] = menu;
    this.count++;}
  }
  public void Add(Menu[] menu)
  { //添菜单项 参数为菜单项数组
    for(int i = 0; i<menu.length; i++)
      if(count < length)
      {this.menu[count] = menu[i]; count++;}
  }
public void Display(Graphics g,Font font)
  {//把菜单在画板上显示出来
   fonttmp = g.getFont();//备份初始Font
   Menu menuTemp = new Menu();
   menuTemp.drawStartMenu(g,left,top,width);//先画出菜单的最上部分
   top += menuTemp.getStartMenuHeight();
    for(int i=0; i<count; i++)//再画出所有的菜单项
    {if(this.menu[i].isLine)//如果菜单项是一条线
      {this.menu[i].drawMenu(g,left,top,width);
       top += menu[i].getHeight();
      }
     else
      {  if(this.selectIndex == i)//如果该菜单被选择
          this.menu[i].drawMenu(g,left,top,width,true,font);
          else//没选中的菜单项
          this.menu[i].drawMenu(g,left,top,width,false,font);
          top += menu[i].getHeight();
    }
     }
  menuTemp.drawEndMenu(g,left,top,width);//菜单组的最下面部分
  this.top = this.topbak;//还原top为初值
  g.setFont(this.fonttmp);//还原Font
  }
  public void Display(Graphics g)
  {this.font = Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_PLAIN,Font.SIZE_SMALL);
    Display(g,this.font);
  }
/////////////////////////////////////////////////////
 public void changeSelectMenu(int keyCode)
   {if(keyCode == -1)//按键为UP键,selectIndex--
     {this.selectIndex--;
            this.selectIndex = this.selectIndex < 0 ?
               this.count - 1 : this.selectIndex;//小于最小值,则等于最大值

           //如果菜单项是直线或是不可用的,则一直selectIndex--,直到不满足前面的条件为止
           while((this.menu[this.selectIndex].isLine || !this.menu[this.selectIndex].enabled)&&this.selectIndex>0)
           {this.selectIndex--;
            this.selectIndex = this.selectIndex < 0 ?
            this.count - 1 : this.selectIndex;
           }
           if((this.menu[this.selectIndex].isLine|| !this.menu[this.selectIndex].enabled))
           {this.selectIndex--;
          this.selectIndex = this.selectIndex < 0 ?
          this.count - 1 : this.selectIndex;
         }

     }
    else if (keyCode == -2)//按键为DOWN键,selectIndex++
    {this.selectIndex++;
            this.selectIndex = this.selectIndex > this.count - 1 ?
                 0: this.selectIndex;//大于最大值,则等于最小值
             //如果菜单项是直线或是不可用的,则一直selectIndex++,直到不满足前面的条件为止
             while((this.menu[this.selectIndex].isLine||!this.menu[this.selectIndex].enabled)&&this.selectIndex<this.count-1)
             {   this.selectIndex++;
                 this.selectIndex = this.selectIndex > this.count - 1 ?
                    0: this.selectIndex;
             }
           if((this.menu[this.selectIndex].isLine||!this.menu[this.selectIndex].enabled))
           {this.selectIndex++;
                 this.selectIndex = this.selectIndex > this.count - 1 ?
                    0: this.selectIndex;
}
           }
   }
   //设制和得到菜单组的属性
   public void setVisable(boolean visable)
   {this.isVisable = visable;}

   public boolean getVisable()
   {return this.isVisable;}

   public void setFocus(boolean focus)
   {this.isfocus = focus;}

   public boolean getFocus()
   {return this.isfocus;}

   public int getSelectIndex()
   {return this.selectIndex;}
   public void setSelectIndex(int index)
   {this.selectIndex = index;}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -