现在的位置: 首页 > 综合 > 正文

J2ME中CustomItem实现按钮效果

2013年08月08日 ⁄ 综合 ⁄ 共 2035字 ⁄ 字号 评论关闭

   
                                 未按下效果                                                      按下效果
/**
 * @作者 Jcuckoo
 * @创建日期 2009-5-11
 * @版本 V 1.0
 */

public class MyCustomItem extends CustomItem {
 //指定按钮上面的内容
 private String str;
 //指定按钮的开始位置
 private int x=0;
 private int y=0;
 //指定按钮的高度与宽度
 private int btnWidth=50;
 private int btnHeight=50;
 //表示按键状态,true为按下,false为松开.
 private boolean keyState=false;
 protected MyCustomItem(String str) {
  super(null);
  this.str=str;
 }
 protected int getMinContentHeight() {
  return btnHeight;
 }
 protected int getMinContentWidth() {
  return btnWidth;
 }
 protected int getPrefContentHeight(int arg0) {
  return getMinContentHeight();
 }

 protected int getPrefContentWidth(int arg0) {
  return getMinContentWidth();
 }
 protected void paint(Graphics g, int w, int h) {
  drawButton(g, str, x, y, w, h);
 }
 private void drawButton(Graphics g,String str,int x,int y,int w,int h){
  if(keyState){
   g.setColor(200,120,20);
   g.drawRect(x, y, w-1, h-1);
   
   g.setColor(150,120,20);
   g.fillRect(x+2, y+2, w-4, h-4);
  }else{
   g.setColor(150,120,20);
   g.drawRect(x, y, w-1, h-1);
   
   g.setColor(200,120,20);
   g.fillRect(x+2, y+2, w-4, h-4);
  }
  g.setColor(0,0,0);
  g.drawString(str, (w-6)/2, (h-8)/2, 0);
 }
 protected void keyPressed(int keyCode) {
  keyState=true;
  repaint();
 }
 protected void keyReleased(int keyCode) {
  keyState=false;
  repaint();
 }
}
-----------------------------------------------------------
/**
 * @作者 Jcuckoo
 * @创建日期 2009-5-11
 * @版本 V 1.0
 */

public class MyCustomItemMIDlet extends MIDlet {
 private Display display;
 private Form form;
 public MyCustomItemMIDlet() {
  display=Display.getDisplay(this);
  form=new Form("自定义按钮测试");
  MyCustomItem btn1=new MyCustomItem("1");
  MyCustomItem btn2=new MyCustomItem("2");
  MyCustomItem btn3=new MyCustomItem("3");
  
  form.append(btn1);
  form.append(btn2);
  form.append(btn3);
 }

 protected void destroyApp(boolean arg0) throws MIDletStateChangeException {

 }
 protected void pauseApp() {

 }
 protected void startApp() throws MIDletStateChangeException {
  display.setCurrent(form);
 }
}
 

 

 

 

 ----------------------------------------------
青岛开发区IT实训基地-青开IT实训基地
----------------------------------------------

抱歉!评论已关闭.