J2ME Midlet 中未显示图像
Image not being displayed in J2ME Midlet
我对 J2ME 编程比较陌生。我被要求构建一个显示 5 个水果名称的程序,当单击 "show" 时,它会显示相应的图像。虽然我已经正确编码了,但是没有显示图像,我得到的只是下一个表格的黑屏。
代码:
import javax.microedition.lcdui.* ;
import javax.microedition.midlet.*;
/**
* @author Ashutosh
*/
public class Midlet extends MIDlet implements CommandListener
{
private Display display ;
private Form f,f1 ;
ChoiceGroup cg ;
private Image image ;
Command cmd = new Command("SHOW" , Command.OK , 1) ;
Command cmd1 = new Command("BACK" , Command.BACK , 1) ;
public Midlet()
{
try
{
image = Image.createImage("Apple.png");
}
catch (Exception e)
{
}
}
public void startApp()
{
f = new Form("Home") ;
f1 = new Form("Show Screen") ;
cg=new ChoiceGroup("Select Apple:",Choice.EXCLUSIVE);
display = Display.getDisplay(this) ;
cg.append("Apple",null) ;
cg.append("Banana",null) ;
cg.append("Cherry",null) ;
cg.append("Kiwi",null) ;
cg.append("Mango",null) ;
f.append(cg) ;
f.addCommand(cmd);
f1.addCommand(cmd1);
display.setCurrent(f);
f.setCommandListener(this);
f1.setCommandListener(this);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void commandAction(Command c, Displayable d)
{
int a = cg.getSelectedIndex() ;
if(c == cmd)
{
display.setCurrent(f1);
switch(a)
{
case 0 : f1.append(image) ;
default : System.exit(0) ;
}
}
if(c == cmd1)
{
display.setCurrent(f);
}
}
}
提前致谢。
如果您的图像与您放置的 midlet 位于同一个包中,请尝试:
image = Image.createImage("/Apple.png");
而不是
image = Image.createImage("Apple.png");
并且不要忘记将 e.printStackTrace();
放入您的 catch 块中,以便在发生错误时您可以找出错误。
更新:
而不是:
display.setCurrent(f1);
switch(a)
{
case 0 : f1.append(image) ;
default : System.exit(0) ;
}
尝试:
switch(a)
{
case 0 : f1.append(image) ;
default : System.exit(0) ;
}
display.setCurrent(f1);
我找到了解决方案。
您必须将您的图片放在"Resources" 目录中,仅在您的IDE.Adding 磁盘上的源文件夹中创建的图片是无关紧要的。
msell 的以下回答有效地解决了这个问题。
我对 J2ME 编程比较陌生。我被要求构建一个显示 5 个水果名称的程序,当单击 "show" 时,它会显示相应的图像。虽然我已经正确编码了,但是没有显示图像,我得到的只是下一个表格的黑屏。
代码:
import javax.microedition.lcdui.* ;
import javax.microedition.midlet.*;
/**
* @author Ashutosh
*/
public class Midlet extends MIDlet implements CommandListener
{
private Display display ;
private Form f,f1 ;
ChoiceGroup cg ;
private Image image ;
Command cmd = new Command("SHOW" , Command.OK , 1) ;
Command cmd1 = new Command("BACK" , Command.BACK , 1) ;
public Midlet()
{
try
{
image = Image.createImage("Apple.png");
}
catch (Exception e)
{
}
}
public void startApp()
{
f = new Form("Home") ;
f1 = new Form("Show Screen") ;
cg=new ChoiceGroup("Select Apple:",Choice.EXCLUSIVE);
display = Display.getDisplay(this) ;
cg.append("Apple",null) ;
cg.append("Banana",null) ;
cg.append("Cherry",null) ;
cg.append("Kiwi",null) ;
cg.append("Mango",null) ;
f.append(cg) ;
f.addCommand(cmd);
f1.addCommand(cmd1);
display.setCurrent(f);
f.setCommandListener(this);
f1.setCommandListener(this);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void commandAction(Command c, Displayable d)
{
int a = cg.getSelectedIndex() ;
if(c == cmd)
{
display.setCurrent(f1);
switch(a)
{
case 0 : f1.append(image) ;
default : System.exit(0) ;
}
}
if(c == cmd1)
{
display.setCurrent(f);
}
}
}
提前致谢。
如果您的图像与您放置的 midlet 位于同一个包中,请尝试:
image = Image.createImage("/Apple.png");
而不是
image = Image.createImage("Apple.png");
并且不要忘记将 e.printStackTrace();
放入您的 catch 块中,以便在发生错误时您可以找出错误。
更新: 而不是:
display.setCurrent(f1);
switch(a)
{
case 0 : f1.append(image) ;
default : System.exit(0) ;
}
尝试:
switch(a)
{
case 0 : f1.append(image) ;
default : System.exit(0) ;
}
display.setCurrent(f1);
我找到了解决方案。 您必须将您的图片放在"Resources" 目录中,仅在您的IDE.Adding 磁盘上的源文件夹中创建的图片是无关紧要的。
msell 的以下回答有效地解决了这个问题。