J2ME-gauge应用(非交互式进度条)

一、非交互式进度条Gauge一般可放入单独的线程处理,因为一般位于工作线程中。

二、效果

三、代码

 

package hello;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;


 

public class HelloMIDlet extends MIDlet implementsCommandListener {

    privateboolean midletPaused = false;

   //<editor-fold defaultstate="collapsed" desc="Generated Fields ">
    privateCommand exitCommand;
    privateCommand okCommand;
    private Formform;
   //</editor-fold>
    privateGauge gauge;
    privateboolean issafeexit;
   
    publicHelloMIDlet() {
    }

   //<editor-fold defaultstate="collapsed" desc="Generated Methods ">
   //</editor-fold>

   //<editor-fold defaultstate="collapsed" desc="Generated Method: initialize ">
   
    private voidinitialize() {
       // write pre-initialize user codehere   
 
       // write post-initialize user code here
    }
   //</editor-fold>

   //<editor-fold defaultstate="collapsed" desc="Generated Method: startMIDlet ">
   
    public voidstartMIDlet() {
       // write pre-action user code here
       switchDisplayable(null, getForm());
       gauge=newGauge("进度条演示,系统正在处理中",false,100,0);//第二个参数表示为非交互式的提示器
       form.append( gauge);
       issafeexit=true;
       // write post-action user codehere
   }
   //</editor-fold>

   //<editor-fold defaultstate="collapsed" desc="Generated Method: resumeMIDlet ">
   
    public voidresumeMIDlet() {
       // write pre-action user code here
 
       // write post-action user code here
    }
   //</editor-fold>

   //<editor-fold defaultstate="collapsed" desc="Generated Method: switchDisplayable ">
   
    public voidswitchDisplayable(Alert alert, Displayable nextDisplayable) {
       // write pre-switch user code here
       Display display = getDisplay();
       if (alert == null) {
           display.setCurrent(nextDisplayable);
       } else {
           display.setCurrent(alert, nextDisplayable);
       }
       // write post-switch user code here
    }
   //</editor-fold>

   //<editor-fold defaultstate="collapsed" desc="Generated Method: commandAction for Displayables">
   
    public voidcommandAction(Command command, Displayable displayable) {
       // write pre-action user code here
       if (displayable == form) {
           if (command == exitCommand) {
               // write pre-action user code here
               exitMIDlet();
               // write post-action user code here
           } else if (command == okCommand) {
               // write pre-action user code here
               form.removeCommand(okCommand);
               new Thread(new  GaugeUpdater()).start();
 
               // write post-action user code here
           }
       }
       // write post-action user code here
    }
   //</editor-fold>


   //<editor-fold defaultstate="collapsed" desc="Generated Getter: exitCommand ">
   
    publicCommand getExitCommand() {
       if (exitCommand == null) {
           // write pre-init user code here
           exitCommand = new Command("/u9000/u51FA", Command.EXIT, 0);
           // write post-init user code here
       }
       return exitCommand;
    }
   //</editor-fold>

   //<editor-fold defaultstate="collapsed" desc="Generated Getter: form ">
   
    public FormgetForm() {
       if (form == null) {
           // write pre-init user code here
           form = new Form("/u8FDB/u5EA6/u6761/u6F14/u793A", new Item[] {});
           form.addCommand(getExitCommand());
           form.addCommand(getOkCommand());
           form.setCommandListener(this);
           // write post-init user code here
       }
       return form;
    }
   //</editor-fold>

 

   //<editor-fold defaultstate="collapsed" desc="Generated Getter: okCommand ">
   
    publicCommand getOkCommand() {
       if (okCommand == null) {
           // write pre-init user code here
           okCommand = new Command("/u5F00/u59CB", Command.OK, 0);
           // write post-init user code here
       }
       return okCommand;
    }
   //</editor-fold>

 

 

   
    publicDisplay getDisplay () {
       return Display.getDisplay(this);
    }

   
    public void exitMIDlet() {
       try{
           if (!issafeexit) destroyApp(false);
           else
           {
                switchDisplayable (null, null);
                destroyApp(true);
                notifyDestroyed(); 
           }
         }
      catch (MIDletStateChangeException Error){
          Alert alert=new Alert("系统正忙","请等会再试",null,AlertType.WARNING);
          alert.setTimeout(1500);
          switchDisplayable(alert,form);          
      }
     
    }

   
    public voidstartApp() {
       if (midletPaused) {
           resumeMIDlet ();
       } else {
           initialize ();
           startMIDlet ();
       }
       midletPaused = false;
    }

   
    public voidpauseApp() {
       midletPaused = true;
    }

   
    public voiddestroyApp(boolean unconditional) throwsMIDletStateChangeException{
       if (!unconditional) throw new MIDletStateChangeException();
    }
   
//新增一个线程处理进度条,定义一个内部类   
class GaugeUpdater  implements Runnable{
   GaugeUpdater(){ }
    public voidrun(){
      issafeexit=false;
      try {
       while(gauge.getValue()<gauge.getMaxValue()){
                   Thread.sleep(500);
                   gauge.setValue(gauge.getValue() + 1);
       }
       issafeexit=true;
       gauge.setLabel("处理完毕可以安全退出了");
       }
      catch (InterruptedException ex) {
                   throw new RuntimeException(ex.getMessage());
      }       
  
     
    }
           
   
}
   
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值