📄 formgaugedemo.java
字号:
package formgaugedemo;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import java.util.Random;
public class FormGaugeDemo extends MIDlet
implements Runnable,ItemStateListener,CommandListener{
private boolean appFinishLoading = false;
private int maxValue =10;
private int alpha =1;
private Alert splashAlert;//充当启动屏幕的alert
private Gauge splashGauge;//显示启动进度的非交互gauge
private Gauge NonInteractiveGauge;//有确定值的非交互gauge
private Gauge NonIndefiniteGauge;//无确定值的非交互gauge
private Gauge ActiveGauge;//交互式gauge
private Random rand;//随机函数,用以模拟随机的状态
private String[] GaugeState={"已停止","更新停止","运行中","更新中"};
private Form mainForm;
Command exitCmd;
Thread sysThread;
public FormGaugeDemo() {
//处理启动屏幕
splashAlert = new Alert(null);
splashGauge = new Gauge(null,false,10,0);
splashAlert.setType(AlertType.INFO);
splashAlert.setTimeout(Alert.FOREVER);
splashAlert.setString("正在加载数据,请稍后……");
splashAlert.setIndicator(splashGauge);
//构造相关对象
ActiveGauge = new Gauge("交互式gauge",true,100,0);
NonInteractiveGauge = new Gauge("有确定值的非交互gauge",false,10,0);
NonIndefiniteGauge = new Gauge("无确定值的非交互gauge",
false,Gauge.INDEFINITE,0);
//设置布局格式
ActiveGauge.setLayout(Item.LAYOUT_EXPAND);//根据屏幕自动扩展宽度
NonInteractiveGauge.setLayout(Item.LAYOUT_EXPAND);//根据屏幕自动扩展宽度
NonIndefiniteGauge.setLayout(Item.LAYOUT_EXPAND);//根据屏幕自动扩展宽度
//在form中添加对象
mainForm.append(ActiveGauge);
mainForm.append(NonInteractiveGauge);
mainForm.append(NonIndefiniteGauge);
mainForm.addCommand(exitCmd);
//为form设置事件监听
mainForm.setItemStateListener(this);
mainForm.setCommandListener(this);
sysThread = new Thread(this);
}
public void startApp(){
Display.getDisplay(this).setCurrent(splashAlert);
sysThread.start();
}
public void pauseApp(){}
public void destroyApp(boolean unconditional){}
//线程执行的任务
public void run(){
while(sysThread!=null){
while(appFinishLoading){
int newValue = NonInteractiveGauge.getValue()+alpha;
if(newValue==maxValue){
alpha=-1;
}
else if(newValue==0){
alpha=1;
}
NonInteractiveGauge.setValue(newValue);
//模拟不确定随机状态,随即产生一种状态(0-3)
int r=(rand.nextInt()>>>1)%4;//去掉负数
try{
sysThread.sleep(1000);
}catch(InterruptedException ite){}
//更新无确定值的非交互gauge值
NonIndefiniteGauge.setValue(r);
//描述无确定值的非交互gauge的当前状态
NonIndefiniteGauge.setLabel("当前状态"+GaugeState[r]);
}
if(appFinishLoading == false){
for(int i=0;i<11;i++){
splashGauge.setValue(i);
try{
sysThread.sleep(1000);
}catch(Exception e){}
}
if(splashGauge.getValue()>=10){
appFinishLoading=true;
Display.getDisplay(this).setCurrent(mainForm);
}
}
}
}
//form内item组件的状态事件处理
public void itemStateChanged(Item item){
if(item==ActiveGauge){
ActiveGauge.setLabel("当前的值为:"+ActiveGauge.getValue());
}
}
//command事件处理
public void commandAction(Command c,Displayable d){
if(c==exitCmd){
quitApp();
}
}
public void quitApp(){
this.destroyApp(true);
this.notifyDestroyed();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -