如何使用 j2me 画布获取用户的输入?这可能吗?

发布于 2024-08-16 10:38:21 字数 3488 浏览 4 评论 0原文

我目前正在尝试学习 J2ME 并构建一个连接四的游戏(你们中的一些人可能知道这是“四连胜”)。除了一件让我发疯的事情之外,我的游戏的所有方面或多或少都已经正常工作了!这当然是从用户那里获取文本!

对于游戏的两人模式,我希望能够允许每个玩家输入他们的名字。我正在努力寻找不使用主 Midlet 的文本输入的工作示例。

例如 java2x.com 上的示例仅使用单个 midlet (没有课程或画布或任何东西)。

就目前情况而言,我的应用程序的主 midlet 启动方法只是打开一个主菜单类:

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

主菜单类如下所示:

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

当从此菜单中选择两人游戏(上述开关中的情况 1)时,我希望出现两个文本框这样我就可以获得两个玩家的名字并存储它们。

解决这个问题的最佳方法是什么?这对于画布来说是可能的吗?你知道我在哪里可以找到相关的例子或者至少可以找到一些可能有帮助的东西吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

被你宠の有点坏 2024-08-23 10:38:21

您可以:
1. 让用户以丑陋的方式输入他的输入 文本框(占据整个屏幕)
2. 使用我很久以前从头开始编写的文本框控件,可以在此处
看起来像这样(显示 3 个文本字段):

alt text

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

昔梦 2024-08-23 10:38:21

我有办法解决!好吧。

我可以在不使用主 midlet 的情况下创建表单:

以下主类是名为 midlet 的源包的一部分(与我的项目非常相似):

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

下一段代码是用户名表单类,即名为 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;
}
}

所以看起来使用画布没有简单的方法,我认为我最好使用“丑陋的表单”,因为它们应该在任何设备上工作。

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.

花开半夏魅人心 2024-08-23 10:38:21

这真是一个棘手的情况。基本上您将需要使用 J2ME 的输入文本小部件(顺便说一下,它看起来很糟糕)。如果不这样做,您最终将不得不实现不同类型的手机键盘背后的所有逻辑,并且您将无法访问字典......您的画布基本上只会捕获击键,而不是文本输入。 ..

对不起。

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.

缘字诀 2024-08-23 10:38:21

在这里,您需要实现自定义项目,您所需要做的就是扩展画布上希望用户/玩家输入他/她的名字的部分到CustomItems,并实现customItems预定义的抽象方法,并编写方法对于击键,可在诺基亚论坛中找到。他们已经解释得很好了。查看诺基亚论坛。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文