这是很好的进度条的模板,里面涉及丰富的Java知识,值得很好研究。如:单例模式,接口等知识。。。

package com.srk.gauge;

import javax.microedition.lcdui.Display;

/**
 *
 * 这是仿照Smart Ticket制作的进度条观察者,这个模型的优点是
 * 1,低耦合度。你可以通过Form,Canvas等来实现这个接口
 * 2,支持可中断的任务,因为背景线程是无法强制性中断的,
 * 所以就 没有了在观察者中回调背景线程相应方法的必要,
 * 如果支持可中断的话,可以让背景线程来查询观察者的isStopped()
 * 3,可以说进度条仅仅将自己绘画在屏幕上,他对后台线程毫不关心
 */
public interface ProgressObserver {
    /**
     * 将进度条复位
     */
    public void reset();

    /**
     * 将进度条设置最大
     */
    public void setMax();

    /*
     * 将自己绘制在屏幕上,如果进度条要开启自身的线程用于自动更新画面,
     * 也在这里构造并开启绘画线程(常用于动画滚动条)
     */
    public void show(Display display);

    /**
     * 滚动条退出命令,如果进度条曾经开启自身的线程用于自动更新画面,
     * (常用于动画滚动条),在这里关闭动画线程
     */
    public void exit();

    /**
     * 更新进度条
     */
    public void updateProgress(Object param1);

    public boolean isStoppable();

    public void setStoppable(boolean stoppable);

    public boolean isStopped();

    public void setStopped(boolean stopped);

    public void setTitle(String title);

    public void setPrompt(String prompt);
}


package com.srk.gauge;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Gauge;

public class ProgressGaugeImpl implements ProgressObserver,
        CommandListener {
    private static final int GAUGE_MAX = 8;
    private static final int GAUGE_LEVELS = 4;
    private static ProgressGaugeImpl pgUI;
    private Form f;
    private Gauge gauge;
    private Command stopCMD;
    boolean stopped;
    boolean stoppable;
    int current;
    protected ProgressGaugeImpl() {
        f = new Form("");
        gauge = new Gauge("", false, GAUGE_MAX, 0);
        stopCMD = new Command("Cancel", Command.STOP, 10);
        f.append(gauge);
        f.setCommandListener(this);
    }

    public static ProgressGaugeImpl getInstance() {
        if (pgUI == null) {
            return new ProgressGaugeImpl();
        }
        return pgUI;
    }

    public void reset() {
        current = 0;
        gauge.setValue(0);
        stopped = false;
        setStoppable(false);
        setTitle("");
        setPrompt("");
    }

    public void updateProgress(Object param1) {
        //这里的参数设计为提示语
        current = (current + 1) % GAUGE_LEVELS;
        gauge.setValue(current * GAUGE_MAX / GAUGE_LEVELS);
        if (param1 != null && param1 instanceof String) {
            setPrompt((String) param1);
        }
    }

    public boolean isStoppable() {
        return stoppable;
    }

    public void setStoppable(boolean stoppable) {
        this.stoppable = stoppable;
        if (stoppable) {
            f.addCommand(stopCMD);
        } else {
            f.removeCommand(stopCMD);
        }
    }

    public boolean isStopped() {
        return stopped;
    }

    public void setStopped(boolean stopped) {
        this.stopped = stopped;
    }

    public void setTitle(String title) {
        f.setTitle(title);
    }

    public void setPrompt(String prompt) {
        gauge.setLabel(prompt);
    }

    public void commandAction(Command arg0, Displayable arg1) {
        if (arg0 == stopCMD) {
            if (isStoppable()) {
                stopped = true;
            } else {
                setPrompt("can't stop!");
            }
        }
    }

    public void show(Display display) {
        display.setCurrent(f);
    }

    public void exit() {
        // 忽略
    }

    public void setMax() {
        gauge.setValue(GAUGE_MAX);
    }
}