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

📄 mainform.java

📁 一个手机新闻查看系统
💻 JAVA
字号:


public class MainForm extends Form implements ItemStateListener,
        CommandListener {
    private ChoiceGroup choice;
    private Command okCommand;
    private Command quitCommand;
    private Command cancelCommand;
    private int state = 0;
    private String key = new String("all");
    private String date = new String("all");
    private TextField keyField;
    private StringItem dateString;
    private TextField yearField;
    private TextField monthField;
    private TextField dayField;
    public MainForm() {
        super("新闻查看器");
        choice = new ChoiceGroup("请选择新闻查看类型", Choice.EXCLUSIVE);
        choice.append("所有新闻", null);
        choice.append("根据关键字查询新闻", null);
        choice.append("根据时间查询新闻", null);
        choice.append("根据关键字和时间查询新闻", null);
        choice.append("今日新闻", null);
        this.setItemStateListener(this);
        this.append(choice);
        okCommand = new Command("确定", Command.OK, 1);
        quitCommand = new Command("退出", Command.EXIT, 2);
        cancelCommand = new Command("取消选择", Command.CANCEL, 2);
        this.addCommand(okCommand);
        this.addCommand(quitCommand);
        this.addCommand(cancelCommand);
        this.setCommandListener(this);
        keyField = new TextField("请输入关键字\n", null, 10, TextField.ANY);
        dateString = new StringItem(null, "请输入查询时间:");
        yearField = new TextField("年:", null, 4, TextField.NUMERIC);
        monthField = new TextField("月:", null, 2, TextField.NUMERIC);
        dayField = new TextField("日:", null, 2, TextField.NUMERIC);
    }

    public void commandAction(Command command, Displayable displayable) {
        if (command == quitCommand) {
            MainMIDlet.getInstance().notifyDestroyed();
        }
        if (command == cancelCommand) {
            choice.setSelectedIndex(0, true);
            choice.notifyStateChanged();
        }
        if (command == okCommand) {
            boolean keystate = false;
            boolean datestate = false;
            if ((state == 1) || (state == 3)) {
                if (!keyField.getString().equals("")) {
                    key = keyField.getString();
                    keystate = true;
                    if (state == 1) {
                        datestate = true;
                    }
                    datestate = true;
                } else {
                    this.errorMessage("请输入关键字.");
                }
            } //if
            if ((state == 2) || (state == 3)) {
                if (!(yearField.getString().equals("")) ||
                    (monthField.getString().equals("")) ||
                    (dayField.getString().equals(""))) {
                    String year = yearField.getString();
                    String month = monthField.getString();
                    String day = dayField.getString();
                    date = year + "-" + month + "-" + day;
                    datestate = true;
                    if (state == 2) {
                        keystate = true;
                    }
                } else {
                    this.errorMessage("请输入完整日期.");
                }
            } //if
            if ((state == 0) || (state == 4)) {
                keystate = true;
                datestate = true;
            }
            if (keystate && datestate) {
                Alert a = new Alert("请稍等");
                a.setType(AlertType.INFO);
                a.setString("正在联网,请稍等");
                a.setTimeout(Alert.FOREVER);
                Display.getDisplay(MainMIDlet.getInstance()).setCurrent(a);
                new QueryThread(key, date, this).start();
            }
        }
    }

    public void itemStateChanged(Item item) {
        String label = item.getLabel();
        if (label.equals("请选择新闻查看类型")) {
            state = choice.getSelectedIndex();
            switch (state) {
            case 0: //所有新闻
                viewAll();
                break;
            case 1: //根据关键字查询新闻
                viewKey();
                break;
            case 2: //根据时间查询新闻
                viewDate();
                break;
            case 3: //根据关键字和时间查询新闻
                viewKeyAndDate();
                break;
            case 4: //今日新闻
                viewToday();
                break;
            } //switch
        } //if
    }

    public void removeAllItem() { //删除多余的组件
        int num = this.size();
        if (num > 1) {
            for (int i = num - 1; i > 0; i--) {
                this.delete(i);
            } //for
        } //if
    }

    public void viewAll() { //所有新闻
        removeAllItem();
        key = new String("all");
        date = new String("all");
    }

    public void viewKey() { //根据关键字查询新闻
        removeAllItem();
        this.append(keyField);
        date = new String("");
    }

    public void viewDate() { //根据时间查询新闻
        removeAllItem();
        key = new String("");
        this.append(dateString);
        this.append(yearField);
        this.append(monthField);
        this.append(dayField);
    }

    public void viewKeyAndDate() { //根据关键字和时间查询新闻
        removeAllItem();
        this.append(keyField);
        this.append(dateString);
        this.append(yearField);
        this.append(monthField);
        this.append(dayField);
    }

    public void viewToday() { //今日新闻
        removeAllItem();
        date = new String("today");
    }

    public void errorMessage(String message) {
        Alert a = new Alert("错误");
        a.setType(AlertType.WARNING);
        a.setString(message);
        a.setTimeout(Alert.FOREVER);
        Display.getDisplay(MainMIDlet.getInstance()).setCurrent(a);
    }
}

⌨️ 快捷键说明

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