J2ME开发入门(一)一个简单通用的菜单类

 

/*
 * AbstractMenuForm.java
 *
 * Created on 2006年12月31日, 上午10:36
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 
*/


package  edu.fjtu.ui;
import  java.util.Vector;

import  javax.microedition.lcdui.Canvas;
import  javax.microedition.lcdui.Font;
import  javax.microedition.lcdui.Graphics;


/**
 *
 * 
@author gaobin
 * 自定义菜单窗体
 * 只需要输入 字符串就可以了
 *
 
*/

public   class   AbstractMenuForm  extends  Canvas {
    
    
//set selected and unselected font
    private Font lowFont  = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_SMALL);
    
private Font highFont = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
    
    
//1 is unselected color
    
//2 is selected color
    
//3 is selected background color
    private int lowColor  = 0x000000FF;
    
private int highColor = 0x00FF0000;
    
private int highBGColor = 0x00CCCCCC;

    
    
private int startHeight = 10;
    
private int fontmaxwidth = 20;
    
private int spacing = 2// spacing between menu items
    private int appearNum = 6;//define number menu appear on desk定义桌面上显示的菜单项数
    
    
private int width;
    
private int height;
    
protected int menuIdx = 0;//当前选中位置
    private int menuCount;
    
protected Vector strVector;
    
    
public AbstractMenuForm(Vector strVector1) {
        width 
= getWidth();
        height 
= getHeight();
        strVector 
= new Vector();
        strVector 
= strVector1;
        menuCount 
= strVector.size();
    }

    
    
public void paint(Graphics g){
        g.setColor(
188,223,155);
        g.fillRect(
0,0,width,height);
        g.setColor(
0,0,0);
        
        
int tempnum = appearNum>menuCount?menuCount:appearNum;
        paint1(g,tempnum);
    }

    
    
    
/* 超复杂  
     * 自己看吧   看不懂也没关系-,-
     * 反正只要接受一个 全部包含字符串的VECTOR就可以了
     * 
*/

    
public void paint1(Graphics g,int tempnum){
        
for(int i=0;i<tempnum;i++{
            
            
if(tempnum-menuIdx>0{
                String s 
= (String)strVector.elementAt(i);
                
if (i==menuIdx) {
                    g.setColor(highBGColor);
                    g.fillRect(
0,startHeight + (highFont.getHeight()+spacing)*i,highFont.stringWidth(s),highFont.getHeight());
                    g.setFont(highFont);
                    g.setColor(highColor);
                    g.drawString(s,
0,startHeight + (highFont.getHeight()+spacing)*i,Graphics.LEFT|Graphics.TOP);
                }
 else {
                    g.setFont(lowFont);
                    g.setColor(lowColor);
                    g.drawString(s,
0,startHeight + (highFont.getHeight()+spacing)*i,Graphics.LEFT|Graphics.TOP);
                }

            }
else{
                
int j = menuIdx-tempnum+i+1;
                String s 
= (String)strVector.elementAt(j);
                
if (j==menuIdx) {
                    g.setColor(highBGColor);
                    g.fillRect(
0,startHeight + (highFont.getHeight()+spacing)*(tempnum-1),highFont.stringWidth(s),highFont.getHeight());
                    g.setFont(highFont);
                    g.setColor(highColor);
                    g.drawString(s,
0,startHeight + (highFont.getHeight()+spacing)*(tempnum-1),Graphics.LEFT|Graphics.TOP);
                }
 else {
                    g.setFont(lowFont);
                    g.setColor(lowColor);
                    g.drawString(s,
0,startHeight + (highFont.getHeight()+spacing)*i,Graphics.LEFT|Graphics.TOP);
                }

            }

        }

    }

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

        repaint();
    }

    
    
//public abstract void initstrVector();
    
    
    
//所有的GET SET方法
    public Font getLowFont() {
        
return lowFont;
    }

    
    
public void setLowFont(Font lowFont) {
        
this.lowFont = lowFont;
    }

    
    
public Font getHighFont() {
        
return highFont;
    }

    
    
public void setHighFont(Font highFont) {
        
this.highFont = highFont;
    }

    
    
public int getLowColor() {
        
return lowColor;
    }

    
    
public void setLowColor(int lowColor) {
        
this.lowColor = lowColor;
    }

    
    
public int getHighColor() {
        
return highColor;
    }

    
    
public void setHighColor(int highColor) {
        
this.highColor = highColor;
    }

    
    
public int getHighBGColor() {
        
return highBGColor;
    }

    
    
public void setHighBGColor(int highBGColor) {
        
this.highBGColor = highBGColor;
    }

    
    
public int getStartHeight() {
        
return startHeight;
    }

    
    
public void setStartHeight(int startHeight) {
        
this.startHeight = startHeight;
    }

    
    
public int getFontmaxwidth() {
        
return fontmaxwidth;
    }

    
    
public void setFontmaxwidth(int fontmaxwidth) {
        
this.fontmaxwidth = fontmaxwidth;
    }

    
    
public int getSpacing() {
        
return spacing;
    }

    
    
public void setSpacing(int spacing) {
        
this.spacing = spacing;
    }

    
    
public int getAppearNum() {
        
return appearNum;
    }

    
    
public void setAppearNum(int appearNum) {
        
this.appearNum = appearNum;
    }

    
}

 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值