当前位置: 代码迷 >> J2ME >> Gauge 有关问题请问
  详细解决方案

Gauge 有关问题请问

热度:4841   发布时间:2013-02-25 21:38:22.0
Gauge 问题请教
各位前辈帮我看一下面的代码,Gauge 怎么增加不到指定的form 里的
下面的代码的意思是如果有form传进来就在指定的form上面显示一个进度条
我是在actionCommand 里调用这个类的。
是不是要刷新这个form还是怎么,这个Gauge一直没有显示出来。
但是如果我在调用Alert返回这个界面后这个Gauge是出来的。
请各位指教,谢谢!在经等待
---------------------------------------------
import javax.microedition.lcdui.*;
public class ThreadWait implements Runnable {
  private int sec;
  private Form form;
  private String msg;
  public ThreadWait(int sec,Form form,String msg){
  this.sec=sec;
  this.form=form;
  this.msg=msg;
  new Thread(this).run();
  }
  public void run(){
  try{
  if (form==null) 
  Thread.sleep(sec*1000);
  else
  {
  Gauge gauge=new Gauge(msg,false,sec*1000,Gauge.CONTINUOUS_IDLE);
  form.append(msg);
  form.append(gauge);
  for (int i=10*sec;i<=1000*sec;i+=10*sec)
  {
  Thread.sleep(10*sec);
  gauge.setValue(i);
  }

  }
  }
  catch (Exception e)
  {
  System.out.println(e);
  }
  }
}

------解决方案--------------------------------------------------------
在你原思路上,修改了下ThreadWait类。


Java code
import javax.microedition.lcdui.*;public class ThreadWait implements Runnable {    private int sec;    private Form form;    private String msg;    public ThreadWait(int sec, Form form, String msg) {        this.sec = sec;        this.form = form;        this.msg = msg;        new Thread(this).start();    }    public void run() {        try {            if (form != null) {                Gauge gauge = new Gauge(msg, false, 100, Gauge.CONTINUOUS_IDLE);                form.append(msg);                int k = form.append(gauge);                while (gauge.getValue() != gauge.getMaxValue()) {                    gauge.setValue(gauge.getValue() + 1);                    Thread.sleep(10 * sec);                }                form.delete(k);            }        } catch (Exception e) {            System.out.println(e);        }    }}
------解决方案--------------------------------------------------------
package prj10_2;

import javax.microedition.lcdui.*

public class frm_MIDLet1 extends Form implements CommandListener,
ItemStateListener, ItemCommandListener {
private static frm_MIDLet1 frm;
private TextField tf_id = new TextField("用户名", "", 20, TextField.ANY);
private TextField tf_pw = new TextField("密 码", "", 20, TextField.NUMERIC
| TextField.PASSWORD);
private Command Cmd_login = new Command("登陆", Command.SCREEN, 1);
private Command Cmd_del = new Command("删除", Command.BACK, 1);

public frm_MIDLet1() {
super("登陆界面");
frm = this;
this.append(tf_id);
this.append(tf_pw);
this.addCommand(Cmd_login);
this.setCommandListener(this);
this.setItemStateListener(this);
}

public void commandAction(Command c, Displayable d) {
if (c == Cmd_login) {
Gauge g = new Gauge("登陆验证", false, 100, 0);
frm.append(g);
thread_gauge tg = new thread_gauge(g);
tg.start();
try {
tg.join();
} catch (Exception ex) {
ex.printStackTrace();
}
System.out.println("验证完毕");
}
}

public void commandAction(Command c, Item i) {
TextField tf = (TextField) i;
int p = tf.getCaretPosition();