Java ME 处理多个屏幕

发布于 2024-10-17 15:28:38 字数 2251 浏览 5 评论 0原文

我正在创建一个应用程序,当应用程序启动时,用户可以选择将文件添加到驻留在存储卡上的应用程序。有一个名为 add new 的按钮;当用户单击添加新时,将出现一个表单,用户将在其中输入文件名并可以添加文件,但添加新文件的命令不起作用,任何人都可以告诉我出了什么问题吗?那里?

我的代码片段是

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.io.file.*;
import javax.microedition.io.Connector;
import java.io.IOException;
import java.util.*;

public class HelloMIDlet
    extends MIDlet 
    implements CommandListener {
  private List list;
  private Alert alert;
  private Display display;
  private Form form;
  private TextField fname,fpath;
  private Command select,remove,add,fadd,back,exit;

  public HelloMIDlet() {
    form=new Form("add new:");
    fname=new TextField("enter File Name","",40,TextField.ANY);
    fpath=new TextField("enter File path","file:///SDCard/",50,TextField.ANY);
    list = new List("Welcome", List.IMPLICIT); 
    remove=new Command("Remove Selected",Command.SCREEN,2);
    exit=new Command("Exit",Command.EXIT,0);
    select=new Command("Select",Command.SCREEN,1);
    add=new Command("Add New",Command.SCREEN,2);
    list.addCommand(exit);
    list.addCommand(select);
    list.addCommand(add);
    list.addCommand(remove);
    form.append(fname);
    form.append(fpath);
    list.setCommandListener(this);
  }
  public void startApp() {

    display=Display.getDisplay(this);
    display.setCurrent(list);
  }

  public void pauseApp() {}

  public void destroyApp(boolean unconditional) {}

  public void commandAction(Command c, Displayable s) {
   if(c==exit)
   {
    notifyDestroyed();
    }
    else if(c==add)
    {
    // display.setCurrent(form);
     addfile();

    }
    else if(c==remove)
    {

    }
    else if(c==back)
    {
        display.setCurrent(list);
    }
    else if(c==fadd)
    {
        alert=new Alert("Open the file:","Would you like to open the current file??",null,null);
        alert.setTimeout(Alert.FOREVER);

    }
    else if(c==list.SELECT_COMMAND)
    {

    }
  }
  public void addfile()
  {fadd=new Command("add File",Command.SCREEN,0);
   back=new Command("Back",Command.BACK,1);
   form.addCommand(fadd);
   form.addCommand(back);

   form.setCommandListener(this);
   display.setCurrent(form); 
  }
}

I am creating an application in which when the application starts, the user has an option to add a file to the application which is residing on memory card.There is a button named add new; when the user clicks on add new a form will appear,in which user will enter the file name and can add the file but the command for adding new file is not working can any one suggest me whats going wrong there?

My code snippet is

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.io.file.*;
import javax.microedition.io.Connector;
import java.io.IOException;
import java.util.*;

public class HelloMIDlet
    extends MIDlet 
    implements CommandListener {
  private List list;
  private Alert alert;
  private Display display;
  private Form form;
  private TextField fname,fpath;
  private Command select,remove,add,fadd,back,exit;

  public HelloMIDlet() {
    form=new Form("add new:");
    fname=new TextField("enter File Name","",40,TextField.ANY);
    fpath=new TextField("enter File path","file:///SDCard/",50,TextField.ANY);
    list = new List("Welcome", List.IMPLICIT); 
    remove=new Command("Remove Selected",Command.SCREEN,2);
    exit=new Command("Exit",Command.EXIT,0);
    select=new Command("Select",Command.SCREEN,1);
    add=new Command("Add New",Command.SCREEN,2);
    list.addCommand(exit);
    list.addCommand(select);
    list.addCommand(add);
    list.addCommand(remove);
    form.append(fname);
    form.append(fpath);
    list.setCommandListener(this);
  }
  public void startApp() {

    display=Display.getDisplay(this);
    display.setCurrent(list);
  }

  public void pauseApp() {}

  public void destroyApp(boolean unconditional) {}

  public void commandAction(Command c, Displayable s) {
   if(c==exit)
   {
    notifyDestroyed();
    }
    else if(c==add)
    {
    // display.setCurrent(form);
     addfile();

    }
    else if(c==remove)
    {

    }
    else if(c==back)
    {
        display.setCurrent(list);
    }
    else if(c==fadd)
    {
        alert=new Alert("Open the file:","Would you like to open the current file??",null,null);
        alert.setTimeout(Alert.FOREVER);

    }
    else if(c==list.SELECT_COMMAND)
    {

    }
  }
  public void addfile()
  {fadd=new Command("add File",Command.SCREEN,0);
   back=new Command("Back",Command.BACK,1);
   form.addCommand(fadd);
   form.addCommand(back);

   form.setCommandListener(this);
   display.setCurrent(form); 
  }
}

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

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

发布评论

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

评论(1

淡莣 2024-10-24 15:28:38

当您选择“添加文件”时,该命令将由 commandAction 触发并处理,具体来说,这段代码:

else if(c==fadd)
{
  alert=new Alert("Open the file:","Would you like to open the current file??",null,null);
  alert.setTimeout(Alert.FOREVER);
  // You were creating a Alert instance, but not showing it, this line below is one you 
  // were missing
  display.setCurrent(alert, list);
}

您可以使用 setCurrent(Alert alert, Displayable nextDisplayable)setCurrent(Displayable nextDisplayable),具体取决于您的需要。

When you select on "add File", the command is triggered and handled by commandAction, specifically, this piece of code:

else if(c==fadd)
{
  alert=new Alert("Open the file:","Would you like to open the current file??",null,null);
  alert.setTimeout(Alert.FOREVER);
  // You were creating a Alert instance, but not showing it, this line below is one you 
  // were missing
  display.setCurrent(alert, list);
}

You can either use setCurrent(Alert alert, Displayable nextDisplayable) or setCurrent(Displayable nextDisplayable), depending on what you need.

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