English 中文(简体)
How do I get input from a user using j2me canvases? is this even possible?
原标题:

I am currently trying to learn J2ME and build a connect four game (some of you might know this as four in a row ). I ve More or less got all of the aspects of my game working, apart from one thing that is driving me mad! This is of course getting the text from the user!

For the two player mode of the game I want to be able to allow each player to enter their name. I am struggling to find a working example of text input that doesn t use the main Midlet.

For example the examples on java2x.com just use a single midlet (no classes or canvases or anything).

As it stands my application s main midlet start method simply opens a main menu class:

    package midlet;

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

public class Main extends MIDlet {
    public void startApp() { 
        MainMenu mm = new MainMenu();
        showScreen(mm);
    }

    public static void showScreen(Displayable screen) {
        Display.getDisplay(instance).setCurrent(screen);
    }

    public void pauseApp() {
    }

    public static void quitApp() {
        instance.notifyDestroyed();
    }

    public void destroyApp(boolean unconditional) {
    }
}

The main menu class is as follows:

 package view;

    import javax.microedition.lcdui.*;
    import lang.*;
    import model.*;
    import midlet.Main;

    public class MainMenu extends List implements CommandListener {

        private Command ok = new Command(StringDefs.currDefs.getString("TEXT_OK"), Command.OK, 1);

        public MainMenu() {
            super(StringDefs.currDefs.getString("TEXT_TITLE"), List.IMPLICIT);
            // we we add in the menu items
            append(StringDefs.currDefs.getString("TEXT_PLAY1"), null);
            append(StringDefs.currDefs.getString("TEXT_PLAY2"), null);
            append(StringDefs.currDefs.getString("TEXT_HIGHSCORETABLE"), null);
            append(StringDefs.currDefs.getString("TEXT_HELP"), null);
            append(StringDefs.currDefs.getString("TEXT_QUIT"), null);
            this.addCommand(ok);
            this.setCommandListener(this);
        }

        public void commandAction(Command c, Displayable d) {
            if (c == ok) {
                int selectedItem = this.getSelectedIndex();
                if (selectedItem != -1) {
                    switch (selectedItem) {
                        case 0:
                            GameBoard gameBoard = new model.GameBoard();
                            GameCanvasOnePlayer board = new GameCanvasOnePlayer(gameBoard);
                            Main.showScreen(board);
                            break;
                        case 1:
                            GameBoard gameBoardTwo = new model.GameBoard();
                            GameCanvasTwoPlayer GameCanvasTwoPlayer = new GameCanvasTwoPlayer(gameBoardTwo);
                            Main.showScreen(GameCanvasTwoPlayer);
                            break;
                        case 2:
                            HighScores hc = new HighScores();
                            midlet.Main.showScreen(hc);
                            break;
                        case 3:
                            Help he = new Help();
                            midlet.Main.showScreen(he);
                            break;
                        case 4:
                            QuitConfirmation qc = new QuitConfirmation();
                            midlet.Main.showScreen(qc);
                            break 
                    }
                }
            }
        }
    }

When a two player game is selected (case 1 in the above switch) from this menu I would like two text boxes to appear so that I can get both player names and store them.

What would be the best way of going about this? is this even possible with canvases? And do you know where I can find a relevant example or at least something which may help?

最佳回答

You can either: 1. Make the user enter his input in an ugly Textbox (which takes the whole screen) 2. Use the textbox control I ve written from scratch a long time ago which is available here and looks something like this (3 Textfields shown):

alt text

问题回答

I ve got a solution! well sort of.

I can create a form without using the main midlet:

The following main class is part of a source package called midlet (much like in my project):

package midlet;

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

public class Main extends MIDlet {
    private static UsernameForm unameForm=new UsernameForm();
    private static MIDlet instance;
    public void startApp() {
        instance=this;
        showScreen(unameForm); // show user name form

    }

    public static String getUsername1() {
        return(unameForm.getUsername1());
    }

    public static String getUsername2() {
        return(unameForm.getUsername2());
    }

    public void pauseApp() {
    }

    public static void showScreen(Displayable d) {
        Display.getDisplay(instance).setCurrent(d); // show next screen

    }

    public void destroyApp(boolean unconditional) {
    }
}

The next bit of code is the username form class that is part of a source package called view:

package view;

import javax.microedition.lcdui.*;

public class UsernameForm extends Form implements CommandListener {
    private String username1="";
    private String username2="";
    private TextField tfUsername1=new javax.microedition.lcdui.TextField("User 1","User1",40,TextField.ANY);
    private TextField tfUsername2=new javax.microedition.lcdui.TextField("User 2","User2",40,TextField.ANY);
    private Command cmdOK=new Command("OK",Command.OK,1);
    public UsernameForm() {
        super("User details");
        append(tfUsername1);
        append(tfUsername2);
        addCommand(cmdOK);
        setCommandListener(this);
    }

public void commandAction(Command cmd,Displayable d) {
    if (cmd==cmdOK) {
        this.setUsername1(tfUsername1.getString());
        this.setUsername2(tfUsername2.getString());
        // TO DO, GO TO NEXT SCREEN
    }
}

/**
 * @return the username1
 */
public String getUsername1() {
    return username1;
}

/**
 * @param username1 the username1 to set
 */
public void setUsername1(String username1) {
    this.username1 = username1;
}

/**
 * @return the username2
 */
public String getUsername2() {
    return username2;
}

/**
 * @param username2 the username2 to set
 */
public void setUsername2(String username2) {
    this.username2 = username2;
}
}

So it looks like there s no easy way of doing it using canvases, I think I am better of using ugly forms instead as they should work whatever the device.

That s a really sticky situation. Basically you will need to use J2ME s input text widget (which by the way looks horrible). If you don t, you ll end up having to implement all the logic behind the different types of phone keyboards and you won t have access to the dictionary... Your canvas will basically only be capturing keystrokes, not text input...

Sorry.

Here you need to, implement custom Items, all you need to do is to extend the part of the canvas where to want the user/player to enter his/her name to the CustomItems, and implement the customItems predefined abstract methods, and write method for Key Strokes and that s available in the nokia forum. They have explained it pretty good. Check out the Nokia forum.





相关问题
add text in http request string url

ok i made a midlet through which i can connect to server pages and get soem information as response. For example i made a midlet through which i acced url: http://example.com/?u=nepal&t=1 Now i ...

Do I have to do a setSize() on a Vector before using it?

Given private final Vector v = new Vector(); //instance variable the following 3 lines are in an instance method in the same class. 1. int length = v.capacity(); 2. int size = v.size(); ...

Is the situation with Java ME improving?

It seems to be the consensus that developing for Java ME is not as cross platform as you might expect, particularly compared to say java SE, but it is difficult to assess how the situation is evolving....

Privileged operations in netbeans mobility

I m writing a Java ME app that will use privileged operations such as messaging. By default the user is prompted to confirm each of these operations, but I would like to run it as a background ...

ClassFormatError: 56 while using hessian in j2me

I am trying to use the hessian j2me implementation @ http://hessian.caucho.com/ using java me sdk 3.0. http://hessian.caucho.com/doc/hessian-overview.xtp#Hessian%20Client%20for%20a%20cell-phone ...

热门标签