Java ME 处理多个屏幕
我正在创建一个应用程序,当应用程序启动时,用户可以选择将文件添加到驻留在存储卡上的应用程序。有一个名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您选择“添加文件”时,该命令将由
commandAction
触发并处理,具体来说,这段代码:您可以使用
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:You can either use
setCurrent(Alert alert, Displayable nextDisplayable)
orsetCurrent(Displayable nextDisplayable)
, depending on what you need.