在 j2me 中设计表单

发布于 2024-12-29 13:07:54 字数 2274 浏览 1 评论 0原文

可能的重复:
在 j2me 中设计表单

我创建了一个带有 str 1 和 str 2 作为 StringItem 的表单并将它们添加到表单中。
然后我向 str1 和 str2 添加了 addCommand() 方法,当我单击“下一步”按钮时,当单击类别 1 时,它应该显示警报为类别 1;当单击类别 2 StringItem 时,它应该显示警报为类别 2。
请提出解决方案
我应该在 Command Action () 方法的 if 循环中编写什么,以便当我单击 str1 时,它应该显示类别 1,而当单击 str2 ... 时,它应该显示类别 2?

代码如下,上述问题在 commandAction 方法中的注释中

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;


public class Menu extends MIDlet implements CommandListener {
    Command Next ;
    Display display;
    Form form;
    StringItem str1,str2;
    public Menu() {
        // TODO Auto-generated constructor stub
        str1=new StringItem("1. ", "Category 1");
        str2=new StringItem("2.", "Category 2");
        form=new Form("Menu");
        form.append(str1);
        form.append(str2);
        Next=new Command("Next",Command.SCREEN, 1);
        str1.addCommand(Next);
        str2.addCommand(Next);
        form.addCommand(Next);
        form.setCommandListener(this);
    }


    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
        // TODO Auto-generated method stub

    }

    protected void pauseApp() {
        // TODO Auto-generated method stub

    }

    protected void startApp() throws MIDletStateChangeException {
        // TODO Auto-generated method stub
        display=Display.getDisplay(this);
        display.setCurrent(form);
    }


    public void commandAction(Command c, Displayable d) {
        // TODO Auto-generated method stub
    if(c==Next)
    {
        if(/* What should I write here? */)
        {
        Alert alert=new Alert(null, "This is ", null, AlertType.INFO);
        display.setCurrent(alert, form);
        }
    }
    }

}

Possible Duplicate:
Desigining a Form in j2me

I have created a Form with str 1 and str 2 as StringItem and added them on Form.
Then i have added addCommand() method to str1 and str2 and when I click on Next button it should display an alert as Category 1 when clicked on category 1 and When category 2 when clicked on category 2 StringItem.
Please suggest a solution
What should I write in if loop of Command Action () method so that When I click on str1 it should display Category 1 and when on str2 ... category 2?

Code is below, above question is in comment within commandAction method

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;


public class Menu extends MIDlet implements CommandListener {
    Command Next ;
    Display display;
    Form form;
    StringItem str1,str2;
    public Menu() {
        // TODO Auto-generated constructor stub
        str1=new StringItem("1. ", "Category 1");
        str2=new StringItem("2.", "Category 2");
        form=new Form("Menu");
        form.append(str1);
        form.append(str2);
        Next=new Command("Next",Command.SCREEN, 1);
        str1.addCommand(Next);
        str2.addCommand(Next);
        form.addCommand(Next);
        form.setCommandListener(this);
    }


    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
        // TODO Auto-generated method stub

    }

    protected void pauseApp() {
        // TODO Auto-generated method stub

    }

    protected void startApp() throws MIDletStateChangeException {
        // TODO Auto-generated method stub
        display=Display.getDisplay(this);
        display.setCurrent(form);
    }


    public void commandAction(Command c, Displayable d) {
        // TODO Auto-generated method stub
    if(c==Next)
    {
        if(/* What should I write here? */)
        {
        Alert alert=new Alert(null, "This is ", null, AlertType.INFO);
        display.setCurrent(alert, form);
        }
    }
    }

}

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

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

发布评论

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

评论(2

潜移默化 2025-01-05 13:07:54

仅 Form 需要 Next 命令,str1 和 str2 不需要。事实上,您可以有一个包含类别的列表。因此,在 Menu() 构造函数

Public Menu() {

display = Display.getDisplay(this);
// Create a multiple choice list
lsPrefs = new List("Categories", List.MULTIPLE);

// Append options, with no associated images
lsPrefs.append("Category-1", null);
lsPrefs.append("Category-2", null);

cmNext = new Command("Next", Command.SCREEN,2);

// Add commands, listen for events

lsPrefs.addCommand(cmNext);
lsPrefs.setCommandListener(this);   
}

和 commandAction 类中,您可以访问由

 public void commandAction(Command c, Displayable s){
  if (c == cmNext)
  {
  boolean selected[] = new boolean[lsPrefs.size()];

  // Fill array indicating whether each element is checked 
  lsPrefs.getSelectedFlags(selected);

  for (int i = 0; i < lsPrefs.size(); i++){
       if(selected[i])
                Alert(....);
  }


}

The Next command is required only for the Form and is not required for str1 and str2 . In fact you can have a List which comprises of categories . So in your Menu() constructor

Public Menu() {

display = Display.getDisplay(this);
// Create a multiple choice list
lsPrefs = new List("Categories", List.MULTIPLE);

// Append options, with no associated images
lsPrefs.append("Category-1", null);
lsPrefs.append("Category-2", null);

cmNext = new Command("Next", Command.SCREEN,2);

// Add commands, listen for events

lsPrefs.addCommand(cmNext);
lsPrefs.setCommandListener(this);   
}

and in your commandAction class you can access the categories selected by

 public void commandAction(Command c, Displayable s){
  if (c == cmNext)
  {
  boolean selected[] = new boolean[lsPrefs.size()];

  // Fill array indicating whether each element is checked 
  lsPrefs.getSelectedFlags(selected);

  for (int i = 0; i < lsPrefs.size(); i++){
       if(selected[i])
                Alert(....);
  }


}
吹梦到西洲 2025-01-05 13:07:54

尝试使用 ChoiceGroup - 以下是如何使用它的示例 http://www.java-tips.org/java-me-tips/midp/how-to-use-choicegroup-in-j2me.html

Try using ChoiceGroup - Here's an example as to how to use it http://www.java-tips.org/java-me-tips/midp/how-to-use-choicegroup-in-j2me.html

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