---
title: "黑莓开发学习入门系列，自己动手实现一个日历软件（五）"
author: "bulktree"
date: "2011-01-20T15:45:00Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2011-01-20T15:45:00Z"
id: 929641
url: "https://bulktree.iteye.com/blog/929641"
---

如何自定义一个自己的UI组件？

创建一个自定义UI，需要继承自现有的组件或者直接继承Field类，通常我们必须实现layout()和paint()方法用于显示想要的UI界面。

1\. layout()方法可在手机屏幕上实现一个具有宽度和高度的区域，调用setExtent(width, height);实现。getPreferredWidth（）、getPreferredHeight（）告诉容器出现在屏幕上合适的高度和宽度

&lt;!--&lt;br /&gt; &lt;br /&gt; Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br /&gt; http://www.CodeHighlighter.com/&lt;br /&gt; &lt;br /&gt; --&gt;public int getPreferredWidth() {  
        return this.getScreen().getWidth() / 7;  
    }

    /\**  
     * Gets the preferred height of the button.  
     \*/  
    public int getPreferredHeight() {  
        return \_labelHeight;  
    }

    protected void layout(int width, int height) {  
        // Calc width.  
        width = getPreferredWidth();

        // Calc height.  
        height =getPreferredHeight();

        // Set dimensions.  
        setExtent(width, height);  
    }

2. paint（）方法使用Graphics对象绘制UI，drawLine, drawRect,drawText

&lt;!--&lt;br /&gt; &lt;br /&gt; Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br /&gt; http://www.CodeHighlighter.com/&lt;br /&gt; &lt;br /&gt; --&gt;protected void paint(Graphics graphics) {  
//        graphics.setColor(0xDDDDDD);  
//        graphics.fillRect( 0, 0, getWidth(), getHeight() );  
        int textX, textY, textWidth;  
        int w = getWidth();  
        if (\_isBorder == 0) {  
            graphics.drawRect(0, 0, w, getHeight());  
        }  
        textX = 4;  
        textY = 2;  
        textWidth = w - 6;  
        graphics.drawText(\_label, textX, textY, (int) (getStyle()   
                & DrawStyle.ELLIPSIS | DrawStyle.HALIGN\_MASK), textWidth);  
    }

3\. 如果要处理键盘和滚轮事件可以实现keyChar()/trackwheelClick()方法

4\. 如果在控件获取到焦点，实现onFocus()方法，可查看黑莓自带的例子

贴上完整的代码：CalenderField.java

&lt;!--&lt;br /&gt; &lt;br /&gt; Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br /&gt; http://www.CodeHighlighter.com/&lt;br /&gt; &lt;br /&gt; --&gt;package org.bulktree.calender;

import net.rim.device.api.ui.\*;

class CalenderField extends Field implements DrawStyle {  
    public static final int RECTANGLE = 1;  
    public static final int TRIANGLE = 2;  
    public static final int OCTAGON = 3;

    private String \_label;  
    private Font \_font;  
    private int \_labelHeight;  
    private int \_isBorder = 0;//是否有边框0有1无  
      
    public CalenderField(String label, int shape, long style) {  
        super(style);  
        \_label = label;  
        \_font = getFont();  
        \_labelHeight = \_font.getHeight();  
    }  
      
    public CalenderField(String lable, int shape, long style, int isBorder) {  
        super(style);  
        \_label = lable;  
        \_font = getFont();  
        \_labelHeight = \_font.getHeight();  
        \_isBorder = isBorder;  
    }

    /\**  
     * Gets the preferred width of the button.  
     \*/  
    public int getPreferredWidth() {  
        return this.getScreen().getWidth() / 7;  
    }

    /\**  
     * Gets the preferred height of the button.  
     \*/  
    public int getPreferredHeight() {  
        return \_labelHeight;  
    }

    protected void layout(int width, int height) {  
        // Calc width.  
        width = getPreferredWidth();

        // Calc height.  
        height =getPreferredHeight();

        // Set dimensions.  
        setExtent(width, height);  
    }

    protected void paint(Graphics graphics) {  
//        graphics.setColor(0xDDDDDD);  
//        graphics.fillRect( 0, 0, getWidth(), getHeight() );  
        int textX, textY, textWidth;  
        int w = getWidth();  
        if (\_isBorder == 0) {  
            graphics.drawRect(0, 0, w, getHeight());  
        }  
        textX = 4;  
        textY = 2;  
        textWidth = w - 6;  
        graphics.drawText(\_label, textX, textY, (int) (getStyle()   
                & DrawStyle.ELLIPSIS | DrawStyle.HALIGN\_MASK), textWidth);  
    }  
}