J2ME 用户自定义控件

用户自定义控件

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 
*/

package hello;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

/**
 * 
@author Administrator
 
*/
public class CustomItemDemo extends MIDlet implements CommandListener{

    
private TextField text;
    
//声明用户自定义组件对象
    private SimpleItem  customitem;
    
private Command exit;
    
    
public void startApp() {
        text
=new TextField("姓名","",30,TextField.ANY);
        Form form
=new Form("SimpleItemMIDlet");
        
//构造用户自定义组件实例
        customitem =new SimpleItem("自定义控件");
        
//将用户自定义组件添加到Form表单中
        form.append(customitem);
        form.append(text);
        exit
=new Command("退出",Command.EXIT,0);
        form.addCommand(exit);
        form.setCommandListener(
this);
        Display.getDisplay(
this).setCurrent(form);
    }

    
public void pauseApp() {
    }

    
public void destroyApp(boolean unconditional) {
    }

    
public void commandAction(Command c, Displayable d) {
        
if (c==exit)
        {
            destroyApp(
false);
            notifyDestroyed();
        }
    }
}



//定义用户自定义控件类
class SimpleItem extends CustomItem {

    
public SimpleItem(String title)
    {
        
super(title);
    }

    
protected int getMinContentWidth() {
       
return 100;
    }

    
protected int getMinContentHeight() {
       
return 60;
    }

    
protected int getPrefContentWidth(int height) {
       
return getMinContentWidth();
    }

    
protected int getPrefContentHeight(int width) {
        
return getMinContentHeight();
    }

    
protected void paint(Graphics g, int w, int h) {
        g.drawRect(
00, w-1, h-1);
        g.setColor(
0x000000ff);
        g.drawString(
"用户自定义控件"000);
    }

    
public void commandAction(Command c,Item i)
    {
        repaint();
    }

}

 

 

 

posted @ 2011-02-20 15:15  jhtchina  阅读(274)  评论(0)    收藏  举报