Rss & SiteMap

课外天地 李树青 http://www.njcie.com

李树青 论坛 南京 财经 课外天地
共1 条记录, 每页显示 10 条, 页签: [1]
[浏览完整版]

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

1楼
admin 发表于: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编辑过]
共1 条记录, 每页显示 10 条, 页签: [1]

Copyright ©2002 - 2016 课外天地.Net
Powered By Dvbbs Version 8.3.0
Processed in .03125 s, 2 queries.