TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
- 一、进度条类
- Gauge控件就是一个进度条,通过图形的方式表达一个程序运行的进度或其它操作的
- 运行过程,Gauge的构造函数如下:
- Gauge(String label,boolean interactive,int maxValue,int initialValue)
- 其中,参数"label"指定了控件的标题,参数"interactive"指定了控件的类型,如果设置
- 为true,则表示为交互类型(用于音量控制等),如设置为false,则为不交互类型,只能通过
- 程序控制进度条。参数"maxValue"指定了进度条的最大值,参数initialValue指定了进度条
- 的初始值。
- import javax.microedition.lcdui.*;
- public class GaugeThread extends Gauge implements Runnable {
- private int maxValue = 10;
- private int add = 1;
- private boolean done = false;
-
- public GaugeThread(String label, int maxValue,
- int initialValue) {
- super(label, false, maxValue, initialValue);
- this.maxValue = maxValue;
- new Thread(this).start();
- }
- public void run() {
- while (!done) {
- int newValue = getValue() + add;
- if (newValue == maxValue) {
- add = -1;
- } else if (newValue == 0) {
- add = 1;
- }
- setValue(newValue);
- try {
- Thread.sleep(1000);
- } catch (InterruptedException err) {
- }
- }
- }
-
- void setDone() {
- done = true;
- }
- }
复制代码
- 二、主程序
- import javax.microedition.lcdui.*;
- import javax.microedition.midlet.*;
- public class gaugeDemo_2 extends MIDlet implements CommandListener {
- private Command exitCommand;
- private Form mainform;
- private GaugeThread gthread;
- public gaugeDemo_2() {
- exitCommand = new Command("Exit", Command.EXIT, 1);
- mainform = new Form("gauge实例");
- mainform.addCommand(exitCommand);
- mainform.setCommandListener(this);
- gthread = new GaugeThread("后台自动增加",40,0);
- //new Thread(gthread).start();
-
- mainform.append(gthread);
- mainform.get(0).setLayout(Item.LAYOUT_EXPAND|Item.LAYOUT_NEWLINE_AFTER);
- }
- protected void startApp() throws MIDletStateChangeException {
- Display.getDisplay(this).setCurrent(mainform);
- }
- protected void pauseApp() {
- }
- protected void destroyApp(boolean p1) {
- gthread.setDone();
- }
- public void commandAction(Command c, Displayable d) {
- if (c == exitCommand) {
- destroyApp(false);
- notifyDestroyed();
- }
- }
- }
- [img]http://img.javaxxz.com/2014/10/10/234833765.jpg[/img]
复制代码
源码下载:http://203.93.208.26/kj/cwb/dir7/gaugeDemo_2.zip |
|