⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gaugedemo.java

📁 人民邮电出版社的《J2ME手机开发入门》全部源代码
💻 JAVA
字号:
/*
 * GaugeDemo.java
 *
 * Created on 2005年3月23日, 下午11:46
 */

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

/**
 *
 * @author  Liu Bin
 * @version
 */
public class GaugeDemo extends MIDlet
        implements CommandListener, ItemStateListener {
    private Display display;
    
    //主菜单中的文字
    private static final String[] strMainMenu = {"交互式Gauge演示",
            "有明确值范围的非交互式Gauge",
            "CONTINUOUS_RUNNING状态的非交互式Gauge",
            "INCREMENTAL_UPDATING状态的非交互式Gauge"
    };
    
    private List lstMainMenu;
    private Form formInteractiveGauge;
    private Form formNonInteractiveGauge;
    private Form formNonInteractiveConRunning;
    private Form formNonInteractiveIncUpdating;
    
    //定义使用的命令按钮
    private Command cmdExit;
    private Command cmdBack;
    private Command cmdSelect;
    
    private Gauge gagVolume;
    private Gauge gagProgress;
    private Gauge gapIncUpdatiing;
    private StringItem siResult;
    
    public GaugeDemo() {
        try {
            nbInit();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    private void nbInit() throws Exception {
        cmdExit = new Command("退出", Command.EXIT, 0);
        cmdBack = new Command("返回", Command.BACK, 1);
        cmdSelect = new Command("选择", Command.SCREEN, 2);
        
        prepareInteractiveGaugeDemo();
        prepareNonInteractiveGaugeDemo();
        prepareNonInteractiveConRunningGaugeDemo();
        prepareNonInteractiveIncUpdatingGaugeDemo();
        
        lstMainMenu = new List("Gauge演示", List.IMPLICIT,
                this.strMainMenu, null);
        lstMainMenu.addCommand(this.cmdExit);
        lstMainMenu.setSelectCommand(this.cmdSelect);
        lstMainMenu.setCommandListener(this);
    }
    
    private void prepareInteractiveGaugeDemo() {
        formInteractiveGauge = new Form("交互式Gauge演示");
        formInteractiveGauge.addCommand(cmdBack);
        formInteractiveGauge.addCommand(cmdExit);
        formInteractiveGauge.setCommandListener(this);
        
        siResult = new StringItem(null, "当前声音大小为:0");
        //添加Gauge
        gagVolume = new Gauge("声音",true, 64, 0);
        gagVolume.setLayout(Item.LAYOUT_2 + Item.LAYOUT_EXPAND + 
                Item.LAYOUT_NEWLINE_AFTER);
        
        formInteractiveGauge.append(gagVolume);
        formInteractiveGauge.append(siResult);
        formInteractiveGauge.setItemStateListener(this);
    }
    
    private void prepareNonInteractiveGaugeDemo() {
        formNonInteractiveGauge = new Form("非交互式Gauge演示");
        formNonInteractiveGauge.addCommand(cmdBack);
        formNonInteractiveGauge.addCommand(cmdExit);
        formNonInteractiveGauge.setCommandListener(this);
        
        //添加Gauge
        gagProgress = new Gauge("正在更新系统...",false, 64, 0);
        gagProgress.setLayout(Item.LAYOUT_2 + Item.LAYOUT_EXPAND);
        
        formNonInteractiveGauge.append(gagProgress);
        formNonInteractiveGauge.setItemStateListener(this);
    }
    
    //创建CONTINUOUS_RUNNING状态的非交互式Gauge演示表单
    private void prepareNonInteractiveConRunningGaugeDemo() {
        formNonInteractiveConRunning = new Form("" +
                "CONTINUOUS_RUNNING状态的非交互式Gauge演示");
        formNonInteractiveConRunning.addCommand(cmdBack);
        formNonInteractiveConRunning.addCommand(cmdExit);
        formNonInteractiveConRunning.setCommandListener(this);
        
        //添加Gauge
        Gauge gagConRunning = new Gauge("正在更新系统...",
                false, Gauge.INDEFINITE, Gauge.CONTINUOUS_RUNNING);
        gagConRunning.setLayout(Item.LAYOUT_2 + Item.LAYOUT_EXPAND);
        
        formNonInteractiveConRunning.append(gagConRunning);
    }
    
    //创建INCREMENTAL_UPDATING状态的非交互式Gauge演示表单
    private void prepareNonInteractiveIncUpdatingGaugeDemo() {
        formNonInteractiveIncUpdating = new Form("" +
                "INCREMENTAL_UPDATING状态的非交互式Gauge演示");
        formNonInteractiveIncUpdating.addCommand(cmdBack);
        formNonInteractiveIncUpdating.addCommand(cmdExit);
        formNonInteractiveIncUpdating.setCommandListener(this);
        
        //添加Gauge
        gapIncUpdatiing = new Gauge("正在更新系统...",
                false, Gauge.INDEFINITE, Gauge.INCREMENTAL_UPDATING);
        gapIncUpdatiing.setLayout(Item.LAYOUT_2 + Item.LAYOUT_EXPAND);
        
        formNonInteractiveIncUpdating.append(gapIncUpdatiing);
    }
    
    public void startApp() {
        display = Display.getDisplay(this);
        display.setCurrent(this.lstMainMenu);
    }
    
    public void pauseApp() {
    }
    
    public void destroyApp(boolean unconditional) {
        notifyDestroyed();
    }
    
    /**
     * 处理命令按钮事件
     */
    public void commandAction(Command cmd, Displayable d) {
        if (cmd == cmdExit) {
            destroyApp(true);
        }
        
        if (cmd == this.cmdSelect) {
            //根据不同的选择进行不同的演示
            int index = this.lstMainMenu.getSelectedIndex();
            switch (index) {
                case 0:
                    //交互式Gauge演示
                    display.setCurrent(this.formInteractiveGauge);
                    break;
                case 1:
                    //非交互式Gauge演示
                    display.setCurrent(this.formNonInteractiveGauge);
                    
                    //更新Gauge的值
                    new Thread(new Runnable() {
                        public void run() {
                            //模拟系统更新过程
                            for (int i=0;i<=gagProgress.getMaxValue();i++) {
                                gagProgress.setValue(i);
                                try {
                                    Thread.sleep(100);
                                } catch (Exception e) {
                                }
                            }
                            Alert alert = new Alert("系统提示",
                                    "系统更新完毕",
                                    null, AlertType.INFO);
                            alert.setTimeout(2000);
                            display.setCurrent(alert, lstMainMenu);
                        }
                    }).start();
                    break;
                case 2:
                    display.setCurrent(this.formNonInteractiveIncUpdating);
                    //更新Gauge的值
                    new Thread(new Runnable() {
                        public void run() {
                            //模拟系统更新过程
                            for (int i=0;i<=100;i++) {
                                gapIncUpdatiing.setValue(Gauge.INCREMENTAL_UPDATING);
                                try {
                                    Thread.sleep(100);
                                } catch (Exception e) {
                                }
                            }
                            Alert alert = new Alert("系统提示",
                                    "系统更新完毕",
                                    null, AlertType.INFO);
                            alert.setTimeout(2000);
                            display.setCurrent(alert, lstMainMenu);
                        }
                    }).start();
                    break;
                case 3:
                    display.setCurrent(this.formNonInteractiveConRunning);
                    break;
            }
        }
        
        if (cmd == this.cmdBack) {
            display.setCurrent(this.lstMainMenu);
        }
    }
    
    /**
     * 处理Item状态改变
     */
    public void itemStateChanged(Item item) {
        String str = item.getLabel();
        if (item == this.gagVolume) {
            str += "  当前声音大小为:" + this.gagVolume.getValue();
            this.siResult.setText(str);
        }
        
        System.out.println(str);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -