WAP之家:为您提供最全最新的WAP技术,CP.SP.3G等行业资讯。 WAP之家交流论坛全新开放 点击进入>>
WAP资讯 | 3G动态 | SP动态 | 运营商动态 | 内容商动态 | 制造商动态 | 论坛讨论>> 每次自动访问
WAP技术 | WAP源码 | 手机编程 | 手机源码 | 无线技术 | J2ME技术 | 手机软件 添加到收藏夹
IVR技术 | SP资料 | SMS MMS技术 | 商业方案 | IVR下载 | 书籍教程 | 工具软件 语言:繁體中文

WAP之家技术文章J2ME技术进阶教程个图形用户界面绘画和事件处理的案例

个图形用户界面绘画和事件处理的案例
作者:j2medev  来源:j2medev.com  发布时间:2006-7-25 8:47:10

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


/*
 * A quick sample of graphics,  commands, and event handling.
 */
public class SampleCanvasMIDlet extends MIDlet implements CommandListener {
    Display display;
    Command exitCommand;
    Command backCommand;
    Command okCommand;
    SampleCanvas sample; // Instance of sample canvas

    List itemMenu;
    List exclusiveList;
    List multipleList;
    TextBox textbox;
    Ticker ticker;
    Alert alert;
    Form form;
    StringItem stringItem;
    ImageItem imageItem;
    Image image;
    TextField textItem;
    ChoiceGroup choiceItem;
    DateField dateItem;
    Gauge gaugeItem;


    public SampleCanvasMIDlet() {
        display = Display.getDisplay(this);
        exitCommand = new Command("Exit", Command.EXIT, 1);
        backCommand = new Command("Back", Command.BACK, 2);
        okCommand = new Command("OK", Command.OK, 3);

        ticker = new Ticker("Select an item to display");
        itemMenu = new List(null, Choice.IMPLICIT);
        itemMenu.append("Canvas"null);
        itemMenu.append("Form"null);
        itemMenu.append("Alert"null);
        itemMenu.append("TextBox"null);
        itemMenu.append("Exclusive List"null);
        itemMenu.append("Multiple Choice"null);

        itemMenu.setCommandListener(this);
        itemMenu.addCommand(exitCommand);
        itemMenu.setTicker(ticker);
        display.setCurrent(itemMenu);
    }

    public void startApp () {
    }

    public void destroyApp (boolean unconditional) {
    }

    public void pauseApp () {
    }

    public void commandAction(Command c, Displayable s) {
        if (c == backCommand) {
            display.setCurrent(itemMenu);
        else if (s == itemMenu) {
            if (c == List.SELECT_COMMAND) {
                // Handle the item sected to be displayed
                int i = itemMenu.getSelectedIndex();
                switch (i) {
                    case 0// Show Sample canvas              display.setCurrent(getCanvas());
                        break;
                    case 1// Show the form
                        display.setCurrent(getForm());
                        break;
                    case 2// Show an alert
                        display.setCurrent(getAlert("Warning"
                                "This window will dismiss in two seconds."));
                        break;
                    case 3// Show TextBox
                        display.setCurrent(getTextBox());
                        break;
                    case 4// Show Exclusive list
                        display.setCurrent(getExclusiveList());
                        break;
                    case 5// Show Multiple List
                        display.setCurrent(getMultipleList());
                        break;
                }
            else if (c == exitCommand) {
                notifyDestroyed();
            }
        else if (s == exclusiveList) {
            int i = exclusiveList.getSelectedIndex();
            String value = exclusiveList.getString(i);
            alert = getAlert("Border selected:", value);
            display.setCurrent(alert, itemMenu);
        else if (s == multipleList) {
            StringBuffer b = new StringBuffer();
            for (int i = 0; i <= 2; i++) {
                if (multipleList.isSelected(i)) {
                    b.append(multipleList.getString(i));
                    b.append("\n");
                }
            }
            alert = getAlert("Colors selected:", b.toString());
            display.setCurrent(alert, itemMenu);
        else if (s == textbox) {
            String value = textbox.getString();
            alert = getAlert("Text Entered:", value);
            display.setCurrent(alert, itemMenu);
        else if (s == form) {
            alert = getAlert("Image options  saved""");
            display.setCurrent(alert, itemMenu);
        }
    }

    SampleCanvas getCanvas() {
        if (sample == null) {
            sample = new SampleCanvas();
            sample.addCommand(backCommand);
            sample.setCommandListener(this);
        }
        return sample;
    }

    List getExclusiveList() {
        if (exclusiveList == null) {
            exclusiveList = new List("Border Style", Choice.EXCLUSIVE);
            exclusiveList.append("None"null);
            exclusiveList.append("Plain"null);
            exclusiveList.append("Fancy"null);
            exclusiveList.addCommand(backCommand);
            exclusiveList.addCommand(okCommand);
            exclusiveList.setCommandListener(this);
        }
        return exclusiveList;
    }

    List getMultipleList() {
        if (multipleList == null) {
            multipleList = new List("Colors to mix", Choice.MULTIPLE);
            multipleList.append("Red"null);
            multipleList.append("Green"null);
            multipleList.append("Blue"null);
            multipleList.addCommand(backCommand);
            multipleList.addCommand(okCommand);
            multipleList.setCommandListener(this);
        }
        return multipleList;
    }

    TextBox getTextBox() {
        if (textbox == null) {
            textbox = new TextBox("Enter a phone number",""40,
          TextField.PHONENUMBER);
            textbox.addCommand(backCommand);
            textbox.addCommand(okCommand);
            textbox.setCommandListener(this);
        }
        return textbox;
    }
    Alert getAlert(String title, String contents) {
        if (alert == null) {
            alert = new Alert(title);
            alert.setType(AlertType.WARNING);
            alert.setTimeout(2000);
      alert.setString(contents)
        else {
            alert.setTitle(title);
            alert.setString(contents);
        }
        return alert;
    }
    Form getForm() {
         if (form == null) {
            form new Form("Options");

            try {     image = Image.createImage("/images/PhotoAlbum.png");
                imageItem = new ImageItem("Preview:", image, 
                            ImageItem.LAYOUT_NEWLINE_BEFORE, "Mountain");
                form.append(imageItem);
            catch (java.io.IOException ex) {
            }

            textItem = new TextField("Title:""Mountain"32,
             TextField.ANY);
            form.append(textItem);
            dateItem = new DateField("Date:", DateField.DATE);
            dateItem.setDate(new java.util.Date());
            form.append(dateItem);

            choiceItem = new ChoiceGroup("Size:", Choice.EXCLUSIVE);
            choiceItem.append("Small"null);
            choiceItem.append("Large"null);
            form.append(choiceItem);
            gaugeItem = new Gauge("Speed:", true, 105);
            form.append(gaugeItem);

            form.addCommand(backCommand);
            form.addCommand(okCommand);
            form.setCommandListener(this);
        }
        return form;
    }
}


class SampleCanvas extends Canvas {
    int     x, y;           // Location of cross hairs
    String  event = "";     // Last key event type
    int     keyCode;        // Last keyCode pressed
    Font    font;           // Font used for drawing text

[1] [2]  下一页

[] [返回上一页] [打 印]
文章评论

用户名: 查看更多评论

分 值:100分 85分 70分 55分 40分 25分 10分 0分

内 容:

         (注“”为必填内容。) 验证码: 验证码,看不清楚?请点击刷新验证码