本文介绍了您如何在j2me项目中组织代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,我要问的是一个愚蠢的问题,但是由于我正在迈入j2me的第一步,所以我必须这样做.我知道您可以在一个MIDlet中拥有多个屏幕,但是当您要拥有大量屏幕(超过10个)时,这似乎不是一个好主意,因为这样最终会导致混乱.所以这是一个问题(不确定是否可能),通常如何在j2me项目中组织代码?还是将所有内容都放在一个MIDlet中,还是像在Web或桌面应用程序中通常那样,将您的代码分成不同的MIDlet(每个屏幕一个)?

Sorry if what I'm about to ask you is a silly question, but since I'm taking my first steps into j2me I have to do it.I know that you can have multiple screens in a single MIDlet, but that doesn't seem to be a good idea when you're going to have a considerable number of screens(more than 10) as it would end up in a complete mess.So here's the question(not sure if possible), how do you usually organize your code in a j2me project? Or do you put everything inside a single MIDlet or do you separate your code into different MIDlets, one per screen, as we usually do in web or desktop applications??

我还注意到,当一个项目具有多个MIDlet并启动仿真器时,就有可能启动每个MIDlet.如果您可以在不同的MIDlet中具有不同的功能,而无需遵循特定的命令就可以运行任何MIDlet,则似乎不太方便,因为MIDlet可能取决于先前MIDlet中读取的数据.

I also noticed that when a project have multiple MIDlets and you launch the emulator, you have the possibility to start each of them. If you can have different functionality in different MIDlets, being able to run any MIDlet without following a specific order,doesn't seem very convenient since a MIDlet might depend on the data that is read in a previous MIDlet.

如果可以将不同的功能分成不同的MIDlet,请问您是否可以对如何做到这一点有一个大致的了解? 总体上需要采取哪些步骤.

Please if it is possible to separate different functionality into different MIDlets , do you think you could give a general idea of how to do it?? Which steps to take in general.

谢谢.

推荐答案

一般规则:不要试图在单个MIDlet中变得复杂:),这是我喜欢java-me的东西,即做一件事. ..

General rule: Don't try to get to complex in a single MIDlet :), something I rather like about java-me, i.e. do one thing well ...

MIDlet对应于一个应用程序,因为它受到生命周期管理.

The MIDlet corresponds to an application, since it is subjected to life cycle management.

在简单的情况下(即在大多数情况下),我也让它实现了CommandListener接口,因此在MVC术语中,它是控制器.控制器逻辑是通过commandAction方法实现的(基本上是组合命令和屏幕上的if then else).

In simple cases (i.e. in most cases) I let it implement the CommandListener interface, too, so in MVC terms it is the controller. The controller logic is implemented in the commandAction method (basically if then else on the combination command and screen).

ListTextBox实例之类的不同屏幕与MVC中的视图相对应.将它们放在单独的* .java文件中.屏幕可以绑定到模型中的对象(例如编辑器),因此可以通过MIDlet的commandAction方法将其传递到屏幕.

The different screens like List and TextBox instances correspond to the views in MVC. Put those in separate *.java files. A screen could be tied to an object from your model (e.g an editor) so pass it to the screen in the MIDlet's commandAction method.

我看过一些示例,其中的屏幕上实现了commandListener,但是对我来说,这是混合的概念,并且通常不太易于维护...

I have seen examples where the screens implement the commandListener, but to me that's mixing concepts and generally less maintainable ...

您可以自由选择实现模型的一部分,但是您可能希望研究javax.microedition.rms包的持久性,并且有联系人和日历项的持久实现...

You are fairly free in your choice of implementing the model part, however you might want to look into the javax.microedition.rms package for persistence and there are persistent implementations for contacts and calendar entries ...

假设您具有以下域对象类(DomainObject.java):

Let's say you have the following domain object class (DomainObject.java):

package mvc.midlet;

public class DomainObject {
    public String name = "";
    public String street = "";
    public String phone = "";
}

,您希望一个应用最多创建10个对象,并带有概述屏幕(对象列表)和对象的编辑器.

and you want an app to create up to 10 objects, with an overview screen (a list of object) and an editor for the oject.

这是概述屏幕(Overview.java)的代码:

Here is the code for the overview screen (Overview.java):

package mvc.midlet;

import javax.microedition.lcdui.List;

public class Overview extends List {

    public static Overview create(DomainObject[] data) {
        int i = 0;
        for(; i < data.length; i++) {
            if(data[i] == null) break;
        }
        String[] names = new String[i];
        for(int j = 0; j < i; j++) {
            names[j] = data[j].name;
        }
        return new Overview(names);
    }

    protected Overview(String names[]) {
        super("Overview", IMPLICIT, names, null);
    }

}

这是编辑器代码(Editor.java):

and here is the editor code (Editor.java):

package mvc.midlet;

import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.TextField;

public class Editor extends Form {

    public static Editor create(DomainObject object, boolean isNew) {
        return new Editor(object, isNew);
    }

    private final DomainObject object;
    public final boolean isNew;
    public DomainObject getObject() {
        return object;
    }

    private final TextField name;
    private final TextField street;
    private final TextField phone;

    protected Editor(DomainObject object, boolean isNew) {
        super("Edit");
        this.object = object;
        this.isNew = isNew;
        this.name = new TextField("Name", object.name, 10, TextField.INITIAL_CAPS_WORD);
        this.append(name);
        this.street = new TextField("Street", object.street, 10, TextField.INITIAL_CAPS_WORD);
        this.append(street);
        this.phone = new TextField("Phone", object.phone, 10, TextField.PHONENUMBER);
        this.append(phone);
    }

    public String getName() {
        return name.getString();
    }
    public String getStreet() {
        return street.getString();
    }
    public String getPhone() {
        return phone.getString();
    }
    public void saveValues() {
        object.name = getName();
        object.street = getStreet();
        object.phone = getPhone();
    }
}

最后是控制这一切的MIDlet(MvcMIDlet.java):

And finally the MIDlet that controls it all (MvcMIDlet.java):

/**
 *
 */
package mvc.midlet;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

/**
 * @author cm
 *
 */
public class MvcMIDlet extends MIDlet implements CommandListener {

    private Command add = new Command("New", Command.SCREEN, 0x01);
    private Command edit= new Command("Edit", Command.SCREEN, 0x01);
    private Command exit= new Command("Exit", Command.EXIT, 0x01);
    private Command ok= new Command("OK", Command.OK, 0x01);

    DomainObject[] data = new DomainObject[10];
    DomainObject current = null;

    /**
     * Initialize some sample data
     */
    public MvcMIDlet() {
        data[0] = new DomainObject();
        data[0].name = "Hans";
        data[1] = new DomainObject();
        data[1].name = "Franz";
    }

    protected void startApp() throws MIDletStateChangeException {
        showOverview();
    }

    /***
     * create an overview, add commands and show it
     */
    private void showOverview() {
        Overview overview = Overview.create(data);
        overview.addCommand(edit);
        overview.addCommand(add);
        overview.addCommand(exit);
        overview.setCommandListener(this);
        Display.getDisplay(this).setCurrent(overview);
    }

    /***
     * create an editor for the given object, add commands and show it
     */
    private void showEditor(DomainObject object, boolean isNew) {
        Editor editor = Editor.create(object, isNew);
        editor.addCommand(ok);
        editor.addCommand(exit);
        editor.setCommandListener(this);
        Display.getDisplay(this).setCurrent(editor);
    }

    public void commandAction(Command c, Displayable d) {
        if(d instanceof Overview) {
            if(c == edit) {
                int i = ((Overview) d).getSelectedIndex();
                showEditor(data[i], false);
            }
            else if(c == add) {
                showEditor(new DomainObject(), true);
            }
            else if(c == exit) {
                this.notifyDestroyed();
            }

        }
        else if(d instanceof Editor) {
            if(c == ok) {
                ((Editor) d).saveValues();
                if(((Editor) d).isNew) {
                    /// search free slot ...
                    int i = 0;
                    for(; i < data.length; i++) {
                        if(data[i] == null) break;
                    }
                    if(i < data.length)  {
                        /// ... and store the new object
                        data[i] = ((Editor)d).getObject();
                    }
                }
                showOverview();
            }
            else if(c == exit) {
                showOverview();
            }
        }
    }
    protected void destroyApp(boolean unconditional)
            throws MIDletStateChangeException {

    }

    protected void pauseApp() {

    }

}

我希望这会有所帮助,不要犹豫,要问...

I hope this helps, don't hesitate to ask ...

这篇关于您如何在j2me项目中组织代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 23:46