j2me竖向滚动菜单的基本做法

public class AppCanvas extends Canvas {

	int screen_width;
	int screen_height;
	String[] select;
	public final static int KEY_UP = -1;
	public final static int KEY_DOWN = -2;

	public AppCanvas() {
		this.setFullScreenMode(true);
		screen_width = this.getWidth();
		screen_height = this.getHeight();
		select = new String[] { "登    录", "注    册", "账户充值", "用户提款", "进出明细",
				"投注查询", "中奖查询", "个人资料", "密码修改" };
	}

	private int itemsOnSceen = 5;//最多显示几条,需要根据屏幕大小判断
	private int index = 0;//select的索引
	private int delta = 0;//index超过itemsOnSceen后的偏移量

	protected void paint(Graphics g) {
		// TODO Auto-generated method stub

		g.setColor(0xffffff);
		g.fillRect(0, 0, screen_width, screen_height);
		
		for (int i = 0; i < ((itemsOnSceen>select.length)?select.length:itemsOnSceen); i++) {
			if (i == index - delta) {
				g.setColor(0xff0000);
			} else {
				g.setColor(0);
			}
			g.drawString(select[i + delta], screen_width >> 1, 40 + 20 * i,
					Graphics.BASELINE | Graphics.HCENTER);
		}
		
	}

	protected void keyPressed(int keyCode) {
		// TODO Auto-generated method stub
		switch (keyCode) {

		case KEY_UP:
			if (index > 0) {
				index--;
			}
			if (index - delta < 0) { //小屏
				delta--;
			}
			break;
		case KEY_DOWN:
			if (index < select.length - 1) {
				index++;
			}
			if (index - delta > itemsOnSceen - 1) { //小屏
				delta++;
			}
			break;
		}
		repaint();

	}
}

你可能感兴趣的:(UP)