Chinaunix首页 | 论坛 | 博客
  • 博客访问: 223993
  • 博文数量: 45
  • 博客积分: 3010
  • 博客等级: 中校
  • 技术积分: 915
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-18 16:03
文章分类

全部博文(45)

文章存档

2011年(1)

2008年(44)

我的朋友

分类: Java

2008-04-01 17:54:58


package j2me.study;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Gauge;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

/**
 * @author Administrator
 *
 */

public class AlertDemo extends MIDlet {

    private final static Command CMD_EXIT = new Command("Exit",Command.EXIT,1);
    
    private final static Command CMD_SHOW = new Command("Show",Command.SCREEN,1);
    
    private final static String[] typeStrings={
        "Alarm","Confirmation","Error","Info","Warning"
    };
    private final static String[] timeoutStrings={
        "2 Seconds","4 Seconds","8 Seconds","Forever"
    };
    private final static int SECOND =1000;
    private Display display;
    
    private boolean firstTime;
    
    private Form mainForm;
    
    public AlertDemo(){
        firstTime = true;
        mainForm = new Form("Alert Options");
    }
    /* (non-Javadoc)
     * @see javax.microedition.midlet.MIDlet#destroyApp(boolean)
     */

    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
        // TODO Auto-generated method stub


    }

    /* (non-Javadoc)
     * @see javax.microedition.midlet.MIDlet#pauseApp()
     */

    protected void pauseApp() {
        // TODO Auto-generated method stub


    }

    /* (non-Javadoc)
     * @see javax.microedition.midlet.MIDlet#startApp()
     */

    protected void startApp() throws MIDletStateChangeException {
        // TODO Auto-generated method stub

        display =Display.getDisplay(this);
        showOption();
    }
    private void showOption(){
        if(firstTime){
            ChoiceGroup types = new ChoiceGroup("Type",ChoiceGroup.POPUP,typeStrings,null);
            mainForm.append(types);
            
            ChoiceGroup timeouts = new ChoiceGroup("Timeout",ChoiceGroup.POPUP,timeoutStrings,null);
            mainForm.append(timeouts);
            
            String[] optionStrings ={"Show Indicator"};
            ChoiceGroup options= new ChoiceGroup("Options",Choice.MULTIPLE,optionStrings,null);
            
            mainForm.append(options);
            mainForm.addCommand(CMD_SHOW);
            mainForm.addCommand(CMD_EXIT);
            mainForm.setCommandListener(new AlerListener(types,timeouts,options));
            firstTime =false;
        }
        display.setCurrent(mainForm);
    }
    private class AlerListener implements CommandListener{
        AlertType[] alertTypes={
                AlertType.ALARM,AlertType.CONFIRMATION,AlertType.ERROR,
                AlertType.INFO,AlertType.WARNING
        };
        ChoiceGroup typesCG;
        int[] timeouts={2 * SECOND,4 * SECOND,8 * SECOND,Alert.FOREVER};
        ChoiceGroup timeoutsCG;
        ChoiceGroup indicatorCG;
        public AlerListener(ChoiceGroup types,ChoiceGroup timeouts,ChoiceGroup indicator){
            typesCG = types;
            timeoutsCG = timeouts;
            indicatorCG = indicator;
        }
        public void commandAction(Command c, Displayable d) {
            // TODO Auto-generated method stub

            if(c == CMD_SHOW){
                int typeIndex = typesCG.getSelectedIndex();
                Alert alert = new Alert("Alert");
                alert.setType(alertTypes[typeIndex]);
                
                int timeoutIndex = timeoutsCG.getSelectedIndex();
                alert.setTimeout(timeouts[timeoutIndex]);
                alert.setString(typeStrings[typeIndex]+" Alert,Running "+timeoutStrings[timeoutIndex]);
                
                boolean [] SelectedFlags = new boolean[1];
                indicatorCG.getSelectedFlags(SelectedFlags);
                
                if(SelectedFlags[0]){
                    Gauge indicator = createIndicator(timeouts[timeoutIndex]);
                    alert.setIndicator(indicator);
                }
                display.setCurrent(alert);
            }else if(c == CMD_EXIT){
                destroyApp(false);
                notifyDestroyed();
            }            
        }
        protected void destroyApp(boolean arg0) {            
        }
    
        protected void pauseApp() {
            // TODO Auto-generated method stub

        }
    private Gauge createIndicator(int maxValue){
        if(maxValue == Alert.FOREVER){
            return new Gauge(null,false,Gauge.INDEFINITE,Gauge.CONTINUOUS_RUNNING);
        }
        final int max = maxValue/SECOND;
        final Gauge indicator = new Gauge(null,false,max,0);
        
        new Thread(){
            public void run(){
                int value =0;
                while(value < max){
                    indicator.setValue(value);
                    ++value;
                    try{
                        Thread.sleep(1000);
                    }catch(InterruptedException ie){
                        
                    }
                }
            }
        }.start();
    
    return indicator;
    }

}
}

阅读(1107) | 评论(0) | 转发(0) |
0

上一篇: TextBox组件应用

下一篇:正确饮食习惯

给主人留下些什么吧!~~