课外天地 李树青学习天地JavaME移动开发课件 → 程序代码——结合Alert的Gauge


  共有13981人关注过本帖树形打印复制链接

主题:程序代码——结合Alert的Gauge

帅哥哟,离线,有人找我吗?
admin
  1楼 博客 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信 管理员
等级:管理员 帖子:1940 积分:26616 威望:0 精华:34 注册:2003/12/30 16:34:32
程序代码——结合Alert的Gauge  发帖心情 Post By:2008/9/25 10:11:12 [只看该作者]

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
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;
import javax.microedition.midlet.MIDlet;

public class Exec extends MIDlet implements CommandListener {
        Display display;
        Gauge gauge = new Gauge("利用多线程演示Gauge的使用方法", false, 10, 0);
        boolean issafeexit = true;
        Form form = new Form("表单");
        Command cmd1 = new Command("开始", Command.OK, 1);
        Command cmd2 = new Command("退出", Command.BACK, 1);
        Thread thread;

        public Exec() {
                display = Display.getDisplay(this);
        }

        public void startApp() {
                form.append(gauge);
                form.addCommand(cmd1);
                form.addCommand(cmd2);
                form.setCommandListener(this);
                display.setCurrent(form);
        }

        public void pauseApp() {
                display.setCurrent(null);
        }

        public void destroyApp(boolean unconditional) {

        }

        public void commandAction(Command c, Displayable arg1) {
                if (c == cmd1) {
                        gauge.setLabel("正在进行处理,可以单击开始按钮重新开始");
                        gauge.setValue(0);
                        form.removeCommand(cmd1);
                        thread = new Thread(new GaugeUpdater());
                        thread.start();
                } else if (c == cmd2) {
                        if (issafeexit == true) {
                                destroyApp(false);
                                notifyDestroyed();
                        } else {
                                Alert alert = new Alert("系统正忙", "请等会再试", null,
                                                AlertType.WARNING);
                                alert.setTimeout(500);
                                display.setCurrent(alert);
                        }
                }
        }

        // 新增一个线程处理进度条,定义一个内部类
        class GaugeUpdater implements Runnable {
                GaugeUpdater() {
                }

                public void run() {
                        issafeexit = false;
                        try {
                                while (gauge.getValue() < gauge.getMaxValue()) {
                                        Thread.sleep(500);
                                        gauge.setValue(gauge.getValue() + 1);
                                }
                                issafeexit = true;
                                gauge.setLabel("处理完毕可以安全退出了");
                                form.addCommand(cmd1);
                        } catch (Exception e) {
                                System.out.println(e.getMessage());
                        }
                }
        }
}

[此贴子已经被作者于2010-12-12 18:32:08编辑过]

 回到顶部