?? gaugealertdemo.java
字號:
/*
* GaugeAlertDemo.java
*
* Created on 2005年4月20日, 上午11:22
*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
*
* @author Liu Bin
* @version
*/
public class GaugeAlertDemo extends MIDlet implements CommandListener {
private Display display;
//定義使用的命令按鈕
private Command cmdExit;
private Command cmdRunTimeout;
private Command cmdRunForever;
private Form form;
private GaugeAlert alert;
public GaugeAlertDemo() {
try {
nbInit();
} catch(Exception e) {
e.printStackTrace();
}
}
private void nbInit() throws Exception {
cmdExit = new Command("退出",Command.EXIT,1);
cmdRunTimeout =new Command("超時運行",Command.SCREEN,2);
cmdRunForever =new Command("永遠運行",Command.SCREEN,2);
form = new Form("在Alert中使用Gauge");
form.append("請選擇菜單命令執行演示");
form.addCommand(cmdExit);
form.addCommand(cmdRunTimeout);
form.addCommand(cmdRunForever);
form.setCommandListener(this);
}
protected void startApp( ) throws MIDletStateChangeException {
display = Display.getDisplay(this);
display.setCurrent(this.form);
}
protected void pauseApp( ) {
}
protected void destroyApp( boolean p1 ) {
notifyDestroyed();
}
/**
* 處理命令按鈕事件
*/
public void commandAction(Command c,Displayable d) {
if (c ==cmdExit) {
destroyApp(false);
notifyDestroyed();
}
//顯示不會自動消失的警告窗口
if (c == cmdRunForever) {
alert = new GaugeAlert("不會自動消失的警告窗口演示",
"數據傳輸中...", null,null,Alert.FOREVER);
Display.getDisplay(this).setCurrent(alert, form);
}
//顯示5秒鐘自動消失的警告窗口
if (c == cmdRunTimeout) {
alert = new GaugeAlert("5秒鐘自動消失的警告窗口演示",
"數據傳輸中...", null,null, 5000);
Display.getDisplay(this).setCurrent(alert, form);
}
if(c ==Alert.DISMISS_COMMAND) {//處理警告窗口退出的命令
//必須手工設置當前需要顯示的對象
Display.getDisplay(this).setCurrent(form);
alert = null;
}
}
}
class GaugeAlert extends Alert {
private int updatePeriod;
/**
* 構造函數
* <p>
* @param title Alert的標題
* @param timeout:自動消失超時時間,以毫秒計算
*/
public GaugeAlert(String title, int timeout) {
this(title, null, null, null, timeout);
}
/**
* 構造函數
* <p>
* @param title Alert的標題
* @param alertText Alert的內容
* @param alertImage alert的Image圖像
* @param alertType alert的類型
* @param timeout 自動消失超時時間,以毫秒計算
*/
public GaugeAlert(String title, String alertText, Image alertImage,
AlertType alertType, int timeout) {
super(title, alertText, alertImage, alertType);
//設置超時自動消失的時間
setTimeout(timeout);
//創建進度條并把進度條添加到警告窗口中
Gauge gauge = createIndicator(timeout);
setIndicator(gauge);
}
//創建進度條
private Gauge createIndicator(final int maxValue) {
//如果警告窗口是永遠顯示的,
//就創建一個在CONTINUOUS_RUNNING狀態的無范圍非交互Gauge
if (maxValue == Alert.FOREVER) {
Gauge gag = new Gauge(null, false, Gauge.INDEFINITE,
Gauge.CONTINUOUS_RUNNING);
return gag;
}
final int step = maxValue>100?maxValue/100:1;
final Gauge indicator = new Gauge(null, false, maxValue, 0);
new Thread() {//創建一個定時更新進度的線程
public void run() {
int value = 0;
while (value < maxValue) {//每次將當前進度增加一
indicator.setValue(value);
value += step;
try {
Thread.sleep(step);
} catch (Exception e) {
}
}
}
}.start();
return indicator;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -