我对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);
}
}
}
提前致谢。
答案 0 :(得分:0)
如果您的图片位于您的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);
答案 1 :(得分:0)
我找到了解决方案。 你必须把你的图像放在"资源"目录,仅在IDE中创建。将映像添加到磁盘上的源文件夹是无关紧要的。
msell的以下答案有效地解决了这个问题。 https://stackoverflow.com/a/1332470/4786292