动态网站制作指南 [  QQ表情  ]
[ 投票调查 ]
[ 企业邮箱 ]
[ 网站空间 ]
网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
ASP源码 | .Net源码 | PHP源码 | JSP源码 | JAVA源码 | CGI源码 | VB源码 | C++源码 | Delphi源码 | PB源码 | VF源码 | 汇编 | 服务器
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,移动开发
文章搜索服务
邮件订阅
输入你的邮件地址,
你将不会错过任何关于:
[ J2EE/J2ME教程 ]的信息

本月文章推荐
.Eclipse与插件(tomcatPlugin Lo.
.在J2ME中访问dotnet Web Services.
.JBOSSAOP学习笔记-AOP基本知识.
.MIDlet Code signing certificat.
.发送word文件.
.Servlet中jdbc应用高级篇之二.
.J2EE架构的6个最佳实践.
.如何在鸡尾酒会上谈论Jini,J2EE.
.MIDP图形用户界面结构分析.
.RMS从入门到精通之一.
.Java语言程序员人生:J2ee的学习流.
.关于ApacheAxis2的Webservice消息.
.Axis2 中的工具wsdl2Java 的使用.
.Jboss下配置EJB.
.使用技巧:J2ME中程序优化的十个.
.JavaOS—Java独立应用平台.
.RMS概念解析与使用指南.
.JSP中tomcat的SQLServer2000数据.
.会话EJB的学习.
.基于MIDP1.0实现通信录.

基于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里面就可以了!

(出处:http://www.knowsky.com)


上一篇:基于MIDP1.0实现RMS容量探测器 人气:546
下一篇:基于MIDP1.0实现动画效果 人气:642
浏览全部J2EE/J2ME的内容 Dreamweaver插件下载 常用网页广告代码全集
  最新网站源码 最新软件下载
2008-12-2 OpenPNE中文 v2.12.5 for win 中
2008-12-2 晴天电影系统(带一键迅雷/自定义
2008-12-2 QQip138闪字程序
2008-12-2 SmartWeb企业智能建站系统 v1.0.2
2008-12-2 梦想不死个人主页 v2009
2008-12-2 开良ASP小偷程序生成器 v1.1
2008-12-2 toolxp.cnalexa世界排名查询 php
2008-12-2 腾讯留言板 v1.3
2008-12-2 OpenPNE中文 v2.12.5 for linux
2008-11-29 Tencent Traveler 4.4
2008-11-29 龙卷风网络收音机 v3.0.0.0
2008-11-29 Intel Chipset Software Install
2008-11-29 TweakVI 1.0 Build 1100
2008-11-29 Opera 9.62 Build 10469
2008-11-29 MPlayer WW编译版 SVN-r28044(20
2008-11-29 NetTools网络工具v1.0.0破解版
2008-11-29 3DGallery三维体验1.1破解版
2008-11-29 SecretBook保密本v1.0破解版
  发表评论
姓 名: 验证码:
内 容:
站长工具:网站收录查询 | Google PR查询 | ALEXA排名查询 | CSS在线编辑器 | OPEN参数生成器 | 弹出式窗口代码产生器 | 密码登录生成器 | 在线按钮生成器 | Meta标签生成器 | 多色彩特效字代码生成器 | 网页代码调试器 | 在线FTP登陆 | Flash取色器 | 配色代码对照表 | 配色辞典 | CSS生成器 | 广告代码 | 框架网页代码生成器 | js/vbs加密 | md5加密 | 进制转换 | UTF-8 转换工具 | 在线调色板 | Html转换js | Html转换asp | Html转换php | Html转换perl
实用工具:汉字翻译拼音 | 拼音字典 | 符号对照表 | 个税计算 | 实时汇率查询换算 | 经典小工具 | 汉字简繁转换 | 普通单位换算 | 公制单位换算 | 生辰老黄历 | 国内电话区号 | 国家代码与域名缩写 | 文字加密解密 | 元素周期表 | 健康查询 | 世界时间 | 万年历 | 二十四节气 | 汉字横竖排版 | 手机号码查询 | 计算器 | ip搜索
业务联系 | 广告刊登 | 频道合作 | 投稿荐稿 | 联系方式 | 加入收藏 | RSS订阅
Copyright © 2000-2009 www.knowsky.com All rights reserved | 沪ICP备05001343号
ホームページ制作 不動産検索システム 求人情報
防水工事·改修工事 フットサル大会 探偵
SEO対策 中国語教室 ホームページ作成