基于MIDP实现Dialog组件

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

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

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

相关推荐

CEH Practical 实战考试真题与答案

CEH Practical 是 EC-Council 推出的Certified Ethical Hacker(CEH)认证项目中的一项高级动手实践考试。它不同于传统的理论考试,侧重于在真实环境中检验考生的实操能力。

sgafdgdagd的博客 969

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

防火墙断网后Adobe软件还弹窗“THIS APP HAS BEEN DISABLED”?原来是科学上网导致的问题!用这个简单方法一键解决!

很多小伙伴在使用上网工具的时候时,突然遇到Adobe弹出“THIS APP HAS BEEN DISABLED”的烦人提示,这个非正版弹窗真的让人头疼!即使你之前尝试过,有时也不奏效,究其原因,原来是因为你使用了代理网络,这时Adobe的流量并不是走本地,而是走代理,导致防火墙的拦截无效。不用慌!今天我来教大家如何通过上网工具,轻松禁用Adobe验证的联网流量,彻底解决这个问题!

qq_34093397的博客 3万+

Java Swing对话框(Dialog)实例

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

ubuntulinuxjava的专栏 1686

采用LM324输出方波、正弦波、三角波

介绍以LM324运放芯片为核心,设计电路连接,实现三角波转化为正弦波,再由正弦波转化为方波输出的简易函数信号发生器的基本功能。本文介绍通过使用NI Multisim 14.0软件仿真模拟搭建所设计的电路,再使用这个软件的示波器进行仿真输出三角波,正弦波,方波。使用软件进行仿真后再到实验室进行调试,将其接入电源,并通过在显示器上观察波形以及数据,得到结果。该电路结构简单,信号的幅度、频率等参数可调,在仿真软件和实验室检测都有较好的性能输出,可替代实验室标准的函数信号发生器完成一般的实验要求。

qq_48771816的博客 7万+

使用Kolla部署多节点Rocky版本OpenStack

openstack命令行客户端使用,post-deploy会在 /etc/kolla/目录下生成文件admin-openrc.sh。每次使用openstack命令时执行source /etc/kolla/admin-openrc.sh(manager节点执行)source /etc/kolla/admin-openrc.sh (将文件内容复制到/etc/profile中,即可一直生效)3.2.2 开始安装kolla-ansible,要带上--ignore-installed PyYAML,否则可能会报错。

gyc1206的博客 1928

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

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

hellogv的专栏 5326

遥感数据处理必看:哨兵1 SLC与GRD预处理差异全解析

本文详细解析了哨兵1号卫星SLC与GRD数据的预处理差异,从数据基础认知到实战应用全流程对比。重点介绍了SLC数据的轨道校正、辐射校正等关键步骤,以及GRD数据的简化处理流程,帮助用户根据应用场景选择合适数据类型。通过SNAP等工具的实际操作示例,提升遥感数据处理效率。

weixin_29035405的博客 339

如何实现j2me对话框

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

mobilechannel的专栏 702

芒果YOLOv8改进165:损失函数angleLoss:即插即用 | 独家首发原创新颖|公开数据集DOTA高效无损涨点0.5% | 旋转检测

YOLOv8改进:旋转检测Loss损失函数angleLoss:即插即用 | 独家首发原创新颖|公开数据集DOTA高效无损涨点0.5%

包括YOLOv5、YOLOv7、YOLOv8等模型改进 1153

图机器学习(9)——图正则化算法

网络信息能够有效约束模型行为,确保相邻节点的输出保持平滑过渡——这种特性在半监督场景中尤其重要,可实现未标注节点的信息传播。更进一步,这种机制也可用于正则化学习过程,从而提升模型在未见样本上的泛化能力。在本节中,我们探讨了在学习阶段应用正则化技术的方法,用于显著提升模型的鲁棒性,使其具备更优异的泛化能力。

盼小辉丶的博客 9012

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

YOLOv12环境配置,手把手教你使用YOLOv12训练自己的数据集和推理(附YOLOv12网络结构图),全文最详细教程

YOLO 系统尊嘟太卷了吧,YOLOv11 还没完明白,YOLOv12 就重磅来袭,YOLOv12 是由纽约州立大学联合中科院在 2025年2月19 日发布,从 YOLOv12 论文题目我们大概就知道做了那些更新 ,下图是YOLOv12 在 COCO 数据集上的性能表现引入区域注意力机制(area-attention):通过引入十字形窗口自我注意机制,沿水平和垂直条纹计算注意力机制,纵横交错的注意机制。如下图所示,区域注意力采用最多简单的等分方法,将特征地图垂直或水平划分为 L 个区域。

在职AI算法工程师,擅长计算机视觉,YOLO目标检测、分割等,擅长web、pyqt界面可视化,好内容持续更新中,来这里跟大家一起学习,共同进步 6万+

安卓自测试题——第七期

第六期试题答案: 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的专栏 1106

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

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

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

配置eclipse

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

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

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

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

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

[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的博客 2906

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、付费专栏及课程。

余额充值