网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > J2EE/J2ME
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.JBoss v3.0 与Tibco JMS的整合.
.J2ME网络程序中移动资费页面的处.
.在Spring中配置Collection.
.使用 Apache Pluto 构建和测试 J.
.J2EE入门教程之四.
.使用JSR-238更快地全球化您的应用.
.Jboss文档(一).
.Java 命名人谈JAVA.
.jBPM在流程中间增加变量.
.XP/J2EE的开发过程.
.JavaBean与EnterpriseJavaBean有.
.优化J2ME程序大小.
.J2EE实用技巧:提升JSP应用程序的.
.J2EE 实战开发EJB.
.分析MIDP的高级事件处理机制.
.MIDP终端模拟之二:高级终端模拟.
.快速创建Webapp.
.J2me性能优化,避免内存溢出小结.
.J2ME最佳实践概述.
.扫描屏幕和颜色渐变的效果.

基于MIDP实现Dialog组件

发表日期:2007-12-23


    在MIDP中没有提供Dialog组件,但是提供了一个Alert。Alert的功能有限,因此写一个Dialog组件是非常有必要的。本文将提供一个基于MIDP的Dialog组件,你可以在应用程序中使用它,功能强大且非常方便。

    当我们开发应用程序的时候,有的时候需要询问用户是不是要继续下面的操作,比如删除一个电话号码,然后根据用户的不同的动作进入不同的流程。这时候我们需要一个像样的Dialog组件,很遗憾MIDP中并没有提供,但是我们可以用Canvas自己写一个。下面将简单介绍这个组件的设计,然后给出测试的MIDlet的源代码。希望对读者有帮助!

      首先我们写一个抽象类Dialog,内容如下

import Javax.microedition.lcdui.*;

public abstract class Dialog
{
    protected Display display;
    protected DialogListener listener;
    protected Displayable restore;
    private int eventID;

    protected Dialog(Display display)
    {
        this.display = display;
    }

    public int getEventID()
    {
        return eventID;
    }


    public void setEventID(int eventID)
    {
        this.eventID = eventID;
    }

    public void dismiss(int code)
    {
        Displayable curr = display.getCurrent();
        if (curr != getDisplayable())
            return;

        if (restore != null)
        {
            display.setCurrent(restore);
        } else
        {
            display.setCurrent(new Form(""));
        }

        if (listener != null)
        {
            listener.dialogDismissed(this, code);
        }
    }

    public void display()
    {
        Displayable curr = display.getCurrent();
        Displayable dialog = getDisplayable();

        if (curr != dialog)
        {
            restore = curr;
            display.setCurrent(dialog);
        }
    }


    public void display(int event)
    {
        Displayable curr = display.getCurrent();
        Displayable dialog = getDisplayable();
        this.eventID = event;

        if (curr != dialog)
        {
            restore = curr;
            display.setCurrent(dialog);
        }
    }

    public DialogListener getDialogListener()
    {
        return listener;
    }

    protected abstract Displayable getDisplayable();

    public void setDialogListener(DialogListener l)
    {
        listener = l;
    }

}

    你需要覆盖getDisplayable()方法返回一个Displayable的对象,当你调用dialog的display()方法的时候,你的YourDialog将会显示在屏幕上,有的时候你可能要传递一个事件值给后面的对象,那么你应该调用方法display(int event)。Dialog可以注册DialogListener,这个接口定义了一个方法,内容如下:

public interface DialogListener
{
    void dialogDismissed(Dialog dialog, int code);
}
当Dialog显示的时候,我们提供给用户的界面是用WaitCanvas实现的,下面是他的代码:

import java.util.*;
import javax.microedition.lcdui.*;

public class WaitCanvas extends Canvas
{

    private int mCount, mMaximum;
    private int mInterval;

    private int mWidth, mHeight, mX, mY, mRadius;
    private String mMessage;
    private boolean run = false;

    public WaitCanvas(String message, boolean run)
    {
        this.mMessage = message;
        mCount = 0;
        mMaximum = 36;
        mInterval = 100;

        mWidth = getWidth();
        mHeight = getHeight();

        // Calculate the radius.
        int halfWidth = (mWidth - mRadius) / 2;
        int halfHeight = (mHeight - mRadius) / 2;
        mRadius = Math.min(halfWidth, halfHeight);

        //   Calculate the location.
        mX = halfWidth - mRadius / 2;
        mY = halfHeight - mRadius / 2;


        //   Create a Timer to update the display.
        if (run)
        {
            TimerTask task = new TimerTask()
            {
                public void run()
                {
                    mCount = (mCount + 1) % mMaximum;
                    repaint();
                }
            };
            Timer timer = new Timer();

            timer.schedule(task, 0, mInterval);
        }
    }

    public void paint(Graphics g)
    {
        int theta = -(mCount * 360 / mMaximum);

        // Clear the whole screen.
        g.setColor(255, 255, 255);
        g.fillRect(0, 0, mWidth, mHeight);

        // Now draw the pinwheel.
        g.setColor(128, 128, 255);
        g.drawArc(mX, mY, mRadius, mRadius, 0, 360);
        g.fillArc(mX, mY, mRadius, mRadius, theta + 90, 90);
        g.fillArc(mX, mY, mRadius, mRadius, theta + 270, 90);

        // Draw the message, if there is a message.
        if (mMessage != null)
        {
            g.drawString(mMessage, mWidth / 2, mHeight, Graphics.BOTTOM
                    Graphics.HCENTER);
        }
    }

}

    过控制boolean run的值可以决定是不是让这个画面动起来。下面两个例子是Dialog的子类,他们实现了它的抽象方法。我直接给出代码:
import javax.microedition.lcdui.*;

public class ConfirmationDialog extends Dialog implements CommandListener
{

    public static final int YES = 0;
    public static final int NO = 1;
    protected Canvas canvas;
    protected Command noCommand;
    protected Command yesCommand;
    private String message;
    private String yesLabel;
    private String noLabel;


    public ConfirmationDialog(Display display, String message)
    {
        this(display, message, null, null);
    }

    public ConfirmationDialog(Display display, String amessage,
            String ayesLabel, String anoLabel)
    {
        super(display);
        this.message = (amessa


  ge == null) ? "继续操作?" : amessage;
        this.yesLabel = (yesLabel == null) ? "确定" : ayesLabel;
        this.noLabel = (noLabel == null) ? "返回" : anoLabel;

        yesCommand = new Command(yesLabel, Command.OK, 1);
        noCommand = new Command(noLabel, Command.CANCEL, 1);

        canvas = new WaitCanvas(message, true);
        canvas.addCommand(yesCommand);
        canvas.addCommand(noCommand);
        canvas.setCommandListener(this);
    }

    public String getMessage()
    {
        return message;
    }

    public void setMessage(String message)
    {
        this.message = message;
    }

    public void commandAction(Command c, Displayable d)
    {
        if (c == yesCommand)
        {
            dismiss(YES);
        } else if (c == noCommand)
        {
            dismiss(NO);
        }
    }

    protected Displayable getDisplayable()
    {
        return canvas;
    }

}


import javax.microedition.lcdui.*;

public class MessageDialog extends Dialog implements CommandListener
{

    public static final int OK = 0;
    protected Command command;
    protected Canvas canvas;
    private String message;
    private String label;

    public MessageDialog(Display display, String message)
    {
        this(display, message, null);
    }


    public MessageDialog(Display display, String amessage, String alabel)
    {
        super(display);
        this.message = (amessage == null)?"完成":amessage;
        this.label = (alabel == null)?"确定":alabel;
        command = new Command(label, Command.OK, 1);
        canvas = new WaitCanvas(message, true);
        canvas.addCommand(command);
        canvas.setCommandListener(this);

    }

    public void commandAction(Command c, Displayable d)
    {
        if (c == command)
        {
            dismiss(OK);
        }
    }

    protected Displayable getDisplayable()
    {
        return canvas;
    }

}

你可以方便的在自己的程序中使用这两个Dialog,你也可以扩展Dialog类实现自己的Dialog,下面是测试这两个Dialog的MIDlet。首先我们看一下它的截图然后给出源代码!
基于MIDP实现Dialog组件(图一)基于MIDP实现Dialog组件(图二)
这里只给出用户选择确定的界面,如果你选择返回那么知识下面的文字改变。

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

public class DialogTest extends MIDlet implements CommandListener,
        DialogListener
{

    private Display display;
    private Form mainForm;
    private ConfirmationDialog confirm;
    private MessageDialog message;

    public static final Command exitCommand = new Command("Exit", Command.EXIT,
            1);

    public DialogTest()
    {
    }

    public void commandAction(Command c, Displayable d)
    {
        if (c == exitCommand)
        {
            exitMIDlet();
        }
    }

    protected void destroyApp(boolean unconditional)
            throws MIDletStateChangeException
    {
        exitMIDlet();
    }


    public void exitMIDlet()
    {
        notifyDestroyed();
    }

    public Display getDisplay()
    {
        return display;
    }

    protected void initMIDlet()
    {
    }

    protected void pauseApp()
    {
    }

    protected void startApp() throws MIDletStateChangeException
    {
        if (display == null)
        {
            display = Display.getDisplay(this);
            initMIDlet();
        }

        confirm = new ConfirmationDialog(display, "继续操作嘛?");
        confirm.setDialogListener(this);
        confirm.display();
    }

    public void dialogDismissed(Dialog d, int code)
    {
        if (d == confirm)
        {

            if (code == ConfirmationDialog.YES)
            {
                message = new MessageDialog(display, "您选择了确定");
            } else
            {
                message = new MessageDialog(display, "您选择了返回");
            }
            message.display();
            message.setDialogListener(this);
        } else if (d == message)
        {

            Form f = new Form(null);
            f.append("退出程序");
            f.addCommand(exitCommand);
            f.setCommandListener(this);
            display.setCurrent(f);
        }
    }


}

上面的整个代码是一个完整的Dialog组件(不包括DialogTest)如果需要使用直接放在你的project里面就可以了!

(出处:)


上一篇:基于MIDP1.0实现RMS容量探测器 人气:583
下一篇:基于MIDP1.0实现动画效果 人气:679
浏览全部J2EE/J2ME的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐