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

📄 custombutton.java

📁 javaME useful code with the J2ME helpful learning cla
💻 JAVA
字号:

import javax.microedition.lcdui.*;

public class CustomButton extends CustomItem implements ItemCommandListener {

	private final static Command CMD_EDIT = new Command("Edit", Command.ITEM, 1);

	private Display display;

	private int width = 80;

	private int height = 30;

	private final static int UPPER = 0;

	private final static int IN = 1;

	private final static int LOWER = 2;

	private int location = UPPER;	
	

	public CustomButton(String title, Display d) {
		super(title);
		display = d;
		setDefaultCommand(CMD_EDIT);
		setItemCommandListener(this);

	}

	protected int getMinContentHeight() {

		return height+2;
	}

	protected int getMinContentWidth() {

		return width+2;
	}

	protected int getPrefContentHeight(int width) {

		return height +2;
	}

	protected int getPrefContentWidth(int height) {

		return width +2;
	}

	protected void paint(Graphics g, int w, int h) {
		int oldColor = g.getColor();
		if (location == IN) {
			
			int px = 0 ,py = 0 ;
			
			g.setColor(255, 255, 253);
		    g.fillRect(px, py, 1, height);
		    g.fillRect(px, py, width, 1);
		    //画中心		   
		    g.setColor(0x004d8ab3);
		    g.fillRect(px + 1, py + 1, width - 1,
		              height - 1);
		    //画红边
		    g.setColor(0x00ff0000);
		    g.fillRect(px + width - 1, py + 1, 1,
		               height -1);
		    g.fillRect(px + 1, py + height - 1,
		              width - 2, 1);
		    //g.setColor(0x00000000);
		    g.drawString("button", width/2, height/2+8 , Graphics.BOTTOM | Graphics.HCENTER);
			
					
			
			
			
		} else {
			 g.setColor(0x004d8ab3);
			g.drawRect(0, 0, width, height);			
			g.drawString("button", width/2, height/2 +8, Graphics.BOTTOM | Graphics.HCENTER);
		}
		g.setColor(oldColor);

	}
	
	protected boolean traverse(int dir, int viewportWidth, int viewportHeight,
			int[] visRect_inout) {

		switch (dir) {

		case Canvas.DOWN:

			if (location == UPPER) {
				location = IN;
			} else {
				repaint();
				location = LOWER;
				return false;
			}

			break;

		case Canvas.UP:

			if (location == LOWER) {
				location = IN;
			} else {
				repaint();
				location = UPPER;
				return false;
			}

			break;
		}
		return true;
	}

	public void commandAction(Command c, Item i) {

		if (c == CMD_EDIT) {

			String text = "已经按下按钮";
			Alert a = new Alert("URL", text, null, AlertType.INFO);
			display.setCurrent(a);
		}
	}
}

⌨️ 快捷键说明

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