[英]Image not being displayed in J2ME Midlet
我对J2ME编程比较陌生。 我被要求构建一个显示5个水果名称的程序,然后单击“显示”,它会显示相应的图像。 尽管我已正确编码,但未显示图像,但我得到的只是下一张表格上的黑屏。
代码:
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);
我找到了解决方案。 您必须将映像放置在仅在IDE中创建的“资源”目录中。将映像添加到磁盘上的源文件夹是没有必要的。
msell的以下回答有效地解决了这个问题。 https://stackoverflow.com/a/1332470/4786292
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.