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

📄 bqmidlet.java

📁 J2ME程序设计实例教程的源码
💻 JAVA
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;

/**
 * 应用程序的主类。
 */
public class BQMIDlet extends MIDlet implements CommandListener {
    private String queryURL = null;
    private Display display;
    private Form mainForm;          //查询主界面
    private ChoiceGroup mode;      //查询方式
    private TextField condition;    //查询条件
    
    private Command cmdExit = new Command("退出", Command.EXIT, 1);
    private Command cmdQuery = new Command("查询", Command.SCREEN, 2);
    private Command cmdBack = new Command("返回", Command.BACK, 1);
    
    private BusQuery busQuery = null;
    private int queryType = ClientQueryMode.BUS;
    
    public BQMIDlet() {
        queryURL = getAppProperty("queryURL");
        busQuery = new BusQuery(this, queryURL);
        display = Display.getDisplay(this);
    }
    
    protected void startApp() {
        createMainForm();
        display.setCurrent(mainForm);
        busQuery.init();
    }
    
    protected void pauseApp() {
    }
    
    protected void destroyApp(boolean unconditional) {
        busQuery.stopQuery();
        busQuery.release();
    }
    
    //创建查询主界面
    private void createMainForm() {
        mainForm = new Form("公交线路查询");
        mode = new ChoiceGroup("查询方式:", Choice.EXCLUSIVE, new String[]{"按公共汽车线路查询", "按站点名称查询"}, null);
        mainForm.append(mode);
        
        mainForm.append(new Spacer(0, 5));
        
        condition = new TextField("查询条件:", "", 20, TextField.ANY);
        mainForm.append(condition);
        
        mainForm.addCommand(cmdExit);
        mainForm.addCommand(cmdQuery);
        mainForm.setCommandListener(this);
        mainForm.setItemStateListener(new ItemStateListener() {
            public void itemStateChanged(Item item) {
                if(mode.getSelectedIndex() == 0) {
                    queryType = ClientQueryMode.BUS;
                }
                else if(mode.getSelectedIndex() == 1) {
                    queryType = ClientQueryMode.STATION;
                }
            }
        });
    }
    
    public void showBuses(Bus[] buses) {
        Form busList = new Form("公共汽车线路:");
        StringItem bus = null;
        for(int i=0; i<buses.length; i++) {
            bus = new StringItem(buses[i].getName(), "路\n" + buses[i].getStations());
            busList.append(bus);
        }
        busList.addCommand(cmdBack);
        busList.setCommandListener(this);
        display.setCurrent(busList);
    }
    
    public void showError(String message) {
        Alert alert = new Alert("查询错误!", message, null, AlertType.ERROR);
        alert.setTimeout(2000);
        display.setCurrent(alert, mainForm);
    }
    
    public void setCurrent(Displayable displayable) {
        display.setCurrent(displayable);
    }
    
    public void backToMain() {
        display.setCurrent(mainForm);
    }
    
    public void commandAction(Command cmd, Displayable displayable) {
        if(cmd == cmdExit) {
            destroyApp(false);
            notifyDestroyed();
        }
        else if(cmd == cmdBack) {
            backToMain();
        }
        else if(cmd == cmdQuery) {
            String cond = condition.getString();
            if(cond == null || cond.equals("")) {
                return;
            }
            busQuery.setCondition(queryType, cond);
            busQuery.execute();
        }
    }
}

⌨️ 快捷键说明

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