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

📄 itemmidlet.java

📁 J2ME in a Nutshell随书源码 图书语言: 简体中文 图书类型: 程序设计 >> 手机开发下载 授权方式: 免费手机开发图书 图书
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            form.append(new ImageItem("Right", red, ImageItem.LAYOUT_RIGHT, null));                        // (4)            form.append(new ImageItem("Default", red, ImageItem.LAYOUT_DEFAULT, null));                        // ImageItems with no labels            // (5)            form.append(new ImageItem(null, blue,                     ImageItem.LAYOUT_NEWLINE_BEFORE |                     ImageItem.LAYOUT_CENTER |                     ImageItem.LAYOUT_NEWLINE_AFTER, null));                        // (6)            form.append(new ImageItem(null, blue,                     ImageItem.LAYOUT_NEWLINE_BEFORE |                     ImageItem.LAYOUT_DEFAULT |                     ImageItem.LAYOUT_NEWLINE_AFTER, null));                        // (7)            form.append(new ImageItem(null, blue,                     ImageItem.LAYOUT_NEWLINE_BEFORE |                     ImageItem.LAYOUT_RIGHT |                     ImageItem.LAYOUT_NEWLINE_AFTER, null));                        // (8)            form.append(new ImageItem(null, blue, ImageItem.LAYOUT_DEFAULT, null));             form.append(new ImageItem(null, blue, ImageItem.LAYOUT_DEFAULT, null));         } catch (IOException ex) {            form.append("Failed to load images");        }                addCommands(form);        return form;    }        // Example for Gauge    private Form createGaugeForm() {        Form form = new Form("Gauge");                form.append(new Gauge(null, true, 100, 50));        form.append(new Gauge(null, true, 100, 25));        form.append(new Gauge(null, false, 100, 50));                addCommands(form);        return form;    }        // Example for ChoiceGroup    private Form createChoiceGroupForm() {        Form form = new Form("ChoiceGroup");                try {            Image red = Image.createImage("/ora/ch4/resources/red.png");            Image green = Image.createImage("/ora/ch4/resources/green.png");            Image blue = Image.createImage("/ora/ch4/resources/blue.png");            // Exclusive choice group            String[] strings = new String[] { "Red", "Green", "Blue" };            Image[] images = new Image[] { red, green, blue };            ChoiceGroup exGroup = new ChoiceGroup("Choose one", ChoiceGroup.EXCLUSIVE,                                                        strings, images);            form.append(exGroup);            // Multiple choice group            ChoiceGroup multiGroup = new ChoiceGroup("Choose any", ChoiceGroup.MULTIPLE);            form.append(multiGroup);            multiGroup.append("Use SSL", null);            multiGroup.append("Reconnect on failure", null);            multiGroup.append("Enable tracing", null);        } catch (IOException ex) {            form.append("Failed to load images");        }                addCommands(form);        return form;    }        // Example for List    private Screen createListExample() {        List list = new List("List", List.IMPLICIT);        try {            Image red = Image.createImage("/ora/ch4/resources/red.png");            Image green = Image.createImage("/ora/ch4/resources/green.png");            Image blue = Image.createImage("/ora/ch4/resources/blue.png");                        list.append("Red", red);            list.append("Green", green);            list.append("Blue", blue);        } catch (IOException ex) {            Form form = new Form("Error");            form.append("Failed to load images");            return form;        }                addCommands(list);        return list;    }        // Example for Alert    private Screen createAlertForm() {        final Form form = new Form("Alert");                // A ChoiceGroup that allows the user to choose between        // a modal Alert or an Alert with a finite timeout.        final ChoiceGroup limitGroup = new ChoiceGroup("Timeout", Choice.EXCLUSIVE);        limitGroup.append("Forever", null);        limitGroup.append("Bounded 5s", null);        form.append(limitGroup);                // A Gauge that allows the user to choose the         // timeout for the Alert. This appears only if        // the "Bounded" option is selected.        final Gauge gauge = new Gauge(null, true, 30, 5);                // A ChoiceGroup that lets the user pick the         // type of Alert        final ChoiceGroup group = createAlertChoiceGroup(true);         form.append(group);                  // A Command that is used to display the Alert        final Command okCommand = new Command("OK", Command.OK, 0);                // Add CommandListener for back/exit commands        // and the OK command, which is specific to this Form        form.addCommand(okCommand);        form.addCommand(exitCommand);        form.addCommand(backCommand);        form.setCommandListener(new CommandListener() {            public void commandAction(Command c, Displayable d) {                if (c == okCommand) {                    // Create and display the Alert                                        // Get the alert type from the                     // second ChoiceGroup                    int selectedType = group.getSelectedIndex();                    AlertType alertType = alertTypes[selectedType];                    String name = alertTypeNames[selectedType];                                        // Get the timeout. This is FOREVER if the                    // first item in the first ChoiceGroup is                    // selected. Otherwise it is the value in                    // the Gauge                    int timeout = Alert.FOREVER;                    String timeoutString = "none";                    if (limitGroup.isSelected(1)) {                        // "Bounded" selected - get the timeout.                        timeout = gauge.getValue() * 1000;                        timeoutString = gauge.getValue() + "s";                                            }                                        // Create the Alert and set the timeout                    Alert alert = new Alert("Alert!",                        "Alert type: " + name + "\nTimeout = " + timeoutString,                        null, alertType);                    alert.setTimeout(timeout);                                        // Display the Alert.                    display.setCurrent(alert);                                               } else {                    // Delegate others to the usual commandAction                    ItemMIDlet.this.commandAction(c, d);                }                            }        });                // Set our own ItemStateListener        form.setItemStateListener(new ItemStateListener() {            public void itemStateChanged(Item item) {                if (item == limitGroup) {                    if (limitGroup.getSelectedIndex() == 0) {                        // Get rid of the Gauge                        form.delete(1);                    } else {                        // Add the Gauge                        form.insert(1, gauge);                    }                } else if (item == gauge) {                                   int value = gauge.getValue();                    limitGroup.set(1, "Bounded " + value + "s", null);                }            }        });        return form;    }                // Example for Sounds    private Screen createSoundsForm() {        final Form form = new Form("Sounds");                // A ChoiceGroup that lets the user pick the         // type of sound        final ChoiceGroup group = createAlertChoiceGroup(false);         form.append(group);          addCommands(form);                // Set our own ItemStateListener        form.setItemStateListener(new ItemStateListener() {            public void itemStateChanged(Item item) {                // Get the alert type from the ChoiceGroup                int selectedType = group.getSelectedIndex();                AlertType alertType = alertTypes[selectedType];                boolean result = alertType.playSound(display);                System.out.println("A sound was " + (result ? "" : "not ")                                    + "played.");                            }        });        return form;    }        // Handles the selection for a Choice     private void handleChoiceSelection(Choice choice) {        int count = choice.size();        boolean[] states = new boolean[count];        int selCount = choice.getSelectedFlags(states);        if (selCount > 0) {            System.out.println("Selected items:");            for (int i = 0; i < count; i++) {                if (states[i]) {                    System.out.println("\t" + choice.getString(i));                }            }        } else {            System.out.println("No selected items.");        }        int selectedIndex = choice.getSelectedIndex();        System.out.println("Selected index is " + selectedIndex);            }        // Creates a ChoiceGroup containing a set of Alert types    private ChoiceGroup createAlertChoiceGroup(boolean includeNone) {        ChoiceGroup group = new ChoiceGroup("Alert Type", Choice.EXCLUSIVE);        int count = includeNone ? alertTypeNames.length : alertTypeNames.length - 1;        for (int i = 0; i < count; i++) {            group.append(alertTypeNames[i], null);        }        return group;    }}

⌨️ 快捷键说明

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