[英]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.