基于MIDP实现Dialog组件

[原创]Swing技巧8:完美的LookAndFeel解决方案 [原创]Swing技巧8:完美的LookAndFeel解决方案Swing技巧8:完美的LookAndFeel解决方案在Swing技巧4:设置系统窗口,边框,标题,最小化,还原,最大化按钮和Swing技巧5:运行中重设LookAndFeel中,我使用if(currentLookAndFeel.getSupportsWindowDecorations()){   frame.set 阅读详情

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

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

      首先我们写一个抽象类Dialog,内容如下
/*
 * Created on 2004-7-10
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author P2800
 *
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */

import javax.microedition.lcdui.*;

//The base class for a set of general-purpose
//dialogs. To use, create a concrete instance
//of this class, set the dialog listener,
//and then call display.

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

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

    /**
     * @return Returns the eventID.
     */
    public int getEventID()
    {
        return eventID;
    }

    /**
     * @param eventID
     *            The eventID to set.
     */
    public void setEventID(int eventID)
    {
        this.eventID = eventID;
    }

    // Dismisses the dialog, restoring the old
    // displayable, if any. This is normally done
    // by the dialog itself in response to commands,
    // but can also be called by the application
    // in response to some other event, such as a
    // timer expiration. The code is passed directly
    // to the dialog listener, if any.

    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);
        }
    }

    // Displays the dialog, saving the current
    // displayable for later restoration.

    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);
        }
    }

    // Returns the registered dialog listener.

    public DialogListener getDialogListener()
    {
        return listener;
    }

    // Subclasses implement this method to return
    // the actual object to display on the screen.

    protected abstract Displayable getDisplayable();

    // Registers a dialog listener.

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

}

你需要覆盖getDisplayable()方法返回一个Displayable的对象,当你调用dialog的display()方法的时候,你的YourDialog将会显示在屏幕上,有的时候你可能要传递一个事件值给后面的对象,那么你应该调用方法display(int event)。Dialog可以注册DialogListener,这个接口定义了一个方法,内容如下:
/*
 * Created on 2004-7-10
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author P2800
 *
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public interface DialogListener
{
    void dialogDismissed(Dialog dialog, int code);
}
当Dialog显示的时候,我们提供给用户的界面是用WaitCanvas实现的,下面是他的代码:
/*
 * Created on 2004-3-14
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */

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

/**
 * @author p2800
 *
 * To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Generation - Code and Comments
 */
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.*;

//Defines a dialog that displays a text
//message and "Yes" and "No" buttons.

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 = (amessage == 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);
    }

    /**
     * @return Returns the message.
     */
    public String getMessage()
    {
        return message;
    }

    /**
     * @param message
     *            The message to set.
     */
    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。首先我们看一下它的截图然后给出源代码!

这里只给出用户选择确定的界面,如果你选择返回那么知识下面的文字改变。

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();
        }

        // First ask the user a question

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

    public void dialogDismissed(Dialog d, int code)
    {
        if (d == confirm)
        {
            // Then give the user a response

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

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

}

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

Verilog仿真必备:FSDB dump命令全解析与实战配置指南 本文深入解析了Verilog仿真中FSDB波形记录的核心命令与实战配置技巧。针对大规模SoC验证场景,详细介绍了如何通过$fsdbDumpvars、$fsdbDumpfile等命令实现信号精准捕获、文件体积控制和动态启停,并提供了智能化的分层dump策略,帮助工程师在保证调试信息完整性的同时,大幅提升验证效率并有效管理波形数据。 阅读详情

相关推荐

【遥感数字图像处理(朱文泉)】各章博文链接汇总及思维导图

遥感数字图像处理(北京师范大学朱文泉教授)课程各章博文笔记汇总,及各章节思维导图

甜子yu的博客 2641

深入解析MPC5643L评估板:电源、时钟与调试接口设计实战

在嵌入式系统开发中,评估板是连接芯片数据手册与可运行实体的关键桥梁,其核心价值在于为微控制器(MCU)提供一个纯净、可验证的“生存环境”。评估板的设计原理围绕三大核心问题展开:为多电压域芯片提供稳定可管理的电源架构、为内核与外设提供灵活精准的时钟源,以及构建可靠标准的调试与程序下载接口。这些基础设计直接决定了系统的实时性、可靠性与开发效率,具有极高的技术价值。尤其在汽车电子和工业控制等对稳定性要求严苛的应用场景中,深入理解评估板的硬件设计,是掌握电源时序管理、去耦电容布局、时钟电路匹配等关键细节的绝佳实践。

无花的专栏 422

A B C三个气象站的小波分析结果解读

在小波分析的过程中,信号的不同频率分量会在不同的尺度上显现,而纵轴上的数值表示信号在相应尺度上的强度或能量。2. **小波方差的横轴(水平轴)**:横轴上的尺度表示用于分析的小波函数的尺度。1. **共同趋势:** 在所有三个点的小波方差图中,随着尺度的增加,方差逐渐增大,尤其是在较大的时间尺度上。3. **尺度30的特殊性:** 在所有点的小波方差图中,尺度30似乎是一个特殊的点,对应着一个较长的时间尺度上的波动或变化。2. **方差的大小:** 方差的大小反映了在相应尺度下信号的波动强度。

weixin_44259522的博客 867

Android dialog弹窗的实现及bug问题

弹窗是APP用来与用户交互提醒的一种表现形式,就本人知道的实现方式有以下几种: 1)activity 2)fragment 3)popupwindow 4)dialog 5)DialogFragment 下面我们进行展开说明: 1、也许有人会问,activity如何实现弹窗的呢?其实可以的,只要你将activity设置为透明主题即可 2、fragment就不用说了,本来碎片的优点就是灵活可复用,实现弹窗不是问题,更何况后面要讲的DialogFragment本身就是继承Fragment的 3、popup...

Welcome 2109

小白学习微信小程序的图像识别和文字识别技术

其中,type参数指定了识别的类型,imageUrl参数指定了图片的地址,success回调函数用于处理识别成功的结果,fail回调函数用于处理识别失败的情况。其中,type参数指定了识别的类型,imageUrl参数指定了图片的地址,success回调函数用于处理识别成功的结果,fail回调函数用于处理识别失败的情况。下面,我们来实现一个文字识别的应用案例,可以识别一张图片中的文字,并将识别结果显示到小程序的页面上。在小程序中,也可以使用微信提供的文字识别接口,实现文字识别的功能开发。

qq_67153941的博客 1649

告别PXE!用iPXE在CentOS 8.5上搭建一个能同时装Win11和Linux的万能网络启动盘

本文详细介绍了如何在CentOS 8.5上使用iPXE搭建支持Windows 11和Linux双系统部署的全能网络启动盘。通过对比传统PXE的局限性,展示了iPXE在HTTP协议支持、多架构适配和智能脚本方面的优势,提供从环境准备、服务配置到智能菜单开发的完整解决方案,显著提升系统部署效率。

weixin_29278805的博客 273

Java Swing对话框(Dialog)实例

来源:http://www.hxstrive.com/article/56.htm     Dialog 是一个带标题和边界的顶层窗口,边界一般用于从用户处获得某种形式的输入。 Dialog 的大小包括边界所指定的任何区域。边界区的维度可以使用 getInsets 方法获得,但是,由于这些维度是依赖于平台的,因此只有通过调用 pack 或 show 将 dialog 设置为可显示的,才能获得...

ubuntulinuxjava的专栏 1686

10、犹豫双模糊TOPSIS算法:原理与实例解析

本文介绍了犹豫双模糊TOPSIS算法的基本原理与应用步骤,结合中国四家纺织公司的案例,详细解析了从构建犹豫双模糊决策矩阵、归一化处理、专家信息聚合,到确定正负理想解、计算距离与相对贴近度系数的完整决策流程。该方法有效应对决策者在评价过程中的犹豫性,适用于财务评估、项目选择等复杂多属性决策场景,具有较强的实用性和推广价值。

yhn45678901的博客 127

J2ME GUI实战之六 ----------LWUIT的Label、CheckBox、RadioButton

 本文来自:http://blog.csdn.net/hellogv/ ,转载必须注明出处!    文章写到这里,想必大家也对LWUIT有个大概的了解了,至少也该知道LWUIT可以做些什么。九宫图只是LWUIT的Button控件的典型应用而已,在LWUIT里面,还有很多在J2SE GUI可以见到的控件,例如文本需要介绍的Label、CheckBox、RadioButton等。    Label一般

hellogv的专栏 5328

深入详解DICOM医学影像定位线相关知识:理解定位线的概念、定位线的作用以及定位线显示和计算原理

深入详解DICOM医学影像定位线相关知识:理解定位线的概念、定位线的作用以及定位线显示和计算原理

编程技术探索者,分享C/C++、C#、Java、数据库等开发经验,聚焦实战技巧与AI兴趣,助力编程爱好者成长。 2058

【Linux】Windows11 安装 WSL2 并运行 Ubuntu 22.04 详细操作步骤

Windows 11 安装 WSL2 和 Ubuntu 22.04 指南 本文详细介绍在 Windows 11 上安装 WSL2 和 Ubuntu 22.04 的完整步骤,包括: WSL2 与 VirtualBox 的性能对比优势 系统版本检查与升级方法 两种安装方式(一键安装和手动安装) Ubuntu 22.04 的安装与配置 存储位置管理及迁移技巧 常用 WSL2 命令速查表 文章还提供了性能优化建议和常见问题解决方案,适合开发者和 Linux 爱好者快速搭建高效的开发环境。

No8g攻城狮的博客 2万+

如何实现j2me对话框

作者:favoyang  文章来源:http://www.j2medev.com/Article/ShowArticle.asp?ArticleID=242内容提要: 在手机这么小的屏幕上开发应用,难点之一就是频繁的屏幕切换。尽管midp2.0的UI部分已经很丰富了,但这些UI部件都是基于事件回调的。这在处理大量的、简单的问答式交互时显得力不从心。本文实现了一个阻塞当前线程的对话框,简要地说,你可

mobilechannel的专栏 702

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

java主类怎么取消_javaMe如何在其他类中引用主类的非成员方法,且改类也不能被实例化...

import javax.microedition.midlet.MIDlet;import javax.microedition.midlet.MIDletStateChangeException;import com.sun.lwuit.Display;public class Main extends MIDlet {public static Main disp; // 引用MIDlet的...

weixin_28705087的博客 116

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

安卓自测试题——第七期

第六期试题答案: 1.B 2.B 3.AB 4.D 5.C 6.C 7.AB 8.D 9.D 10.答:IPC是内部进程通信的简称, 是共享"命名管道"的资源。Android中的IPC机制是为了让Activity和Service之间可以随时的进行交互,故在Android中该机制,只适用于Activity和Service之间的通信,类似于远程方法调用,类似于C/S模式的访

jike084的专栏 1107

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

Android自测题07

1.【单选】在Android中,1PT大概等于多少SP (  D ) A.2.12  B.1.22  C.1.12  D.2.22 2.【单选】在MIDP中,函数Image createImage(Image source)创建的是(  B ) A.静态图像  B.不可变图像  C.可变图像    D.动态图像 3.【单选】自定义对话框时,将视图对象添加到当前对话框的方法是

奋斗、 1482

配置eclipse

1. 导入设备定义你必须至少配置一种设备定义之后才能开始使用EclipseME。请按下列步骤来配置设备定义: 从Eclipse的窗口菜单中选择首选项。 打开左边面板的J2ME选项分支,点击设备管理(Device Management)。 按导入...(Import)按钮。 在接下来的对话框中,选择一个包含无线工具包的根目录,EclipseME将从中查找...

(Never Give Up)--(我的知识点滴) 275

Conmi的正确答案——“java.awt.IllegalComponentStateException: The frame is displayable.”的解决方案

测试系统:WIN10 x64 JDK版本:1.8 IDE工具:NetBeans 8.2 百度了很多都没有解决,对JFRAME了解不多,没详细看代码。 解决方案来自此问题的其中一个评论“https://zhidao.baidu.com/question/475243789.html” 解决方案如下: ...

Conmi的博客 2137

[Unity3d]Player Settings导出设置

Player Settingsis where you define various parameters (platform specific) for the final game that you will build in Unity. Some of these values for example are used in theResolution Dia...

weixin_34137799的博客 2908

Jquery UI dialog 详解 (中文)

1 属性 1.11 autoOpen ,这个属性为true的时候dialog被调用的时候自动打开dialog窗口。当属性为false的时候,一开始隐藏窗口,知道.dialog("open")的时候才弹出dialog窗口。默认为:true。 1.12 初始化例:请注意,$('.selector')是dialog 的类名,在本例中.selector=#dialoag,以后不再说明。 $('.se...

weixin_34413103的博客 2367

WML开发详细指南

  <A title="文章标题:WAP开发入门--搭建开发环境作 者:mingjava更新时间:2004-9-7 15:34:35点击次数:82" href="http://www.j2medev.com/Article_Show.asp?ArticleID=76" target=_blank>WAP开发入门--搭建开发环境讲述了如何搭建WAP应用程序的开发环境,本章将主要介绍

mingjava的专栏 1万+

搭建OTA下载服务器

 OTA的意思是Over The Air,通过无线网络下载和安装J2ME应用程序的方案。搭建OTA服务器是比较简单的,但是做一个完整的OTA的服务器却非常复杂,因为这要设计到软件的管理,下载的计费,安全认证等多个问题。本文主要目的是讲述OTA下载的主要原理,搭建OTA服务器的步骤。     当我们的手机通过无线网络发出请求要求下载相关的软件的时候,通常我们是访问了特定的页面。这可以是wml写的。我

mingjava的专栏 1万+

工欲善其事,必先利其器——搭建J2ME开发环境

      由于本文写作的时候eclipseME的版本还比较低,但是在0.5(如果我没有记错的话)以后安装方式就改变了,截至到11.18号最新的版本应该是0.6.1,如果大家安装高版本的话请参考下面连接中的文章,http://www.j2medev.com/Article_Show.asp?ArticleID=144如果是0.4.x的可以参考本文。       由于WTK并没有提供代码编辑的功能,

mingjava的专栏 1万+

揭开J2ME平台的神秘面纱

      做J2ME应用开发的程序员也许经常会被一些名词或者概念所迷惑,比如Personal Basic Profile和Personal Profile有什么关系?基于CLDC的应用程序能够无修改的移植到基于CDC的设备上嘛?要回答这些问题并不容易,因为你必须首先揭开J2ME平台的神秘面纱。       2000年当J2ME平台的CLDC发布的时候,所有的java开发者都为之兴奋不已。SUN也

mingjava的专栏 1万+
上一篇: 介绍J2ME可选包——PIM OP
下一篇: Record Management System 从入门到精通系列之四
mingjava
博客等级 码龄24年 68粉丝 114原创
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值