我正在尝试为J2ME创建一个接受用户输入并将其字符推入堆栈的应用程序,然后再次弹出它,然后检查字符是否等于(不知道字符)的条件,然后在窗体中显示带有标志语言。
简而言之,是一个将输入的字符串转换为手语的应用程序。
但是我的问题是我运行程序时。
它总是显示最后一个字符...
你能帮我这个忙吗?
顺便说说。这是带有计时器和计时器任务的代码...
private void startTimer()
{
TimerTask task = new TimerTask()
{
public void run()
{
while(!s.empty())
{
char hold;
hold=s.pop();
for(int ii=0; ii<chars.length; ii++)
{
weww=(int)(hold);
//System.out.println(hold);
if(weww==chars[ii])
{
ffm.deleteAll();
ffm.append(img[ii]);
}
}
}
}
};
timer = new Timer();
timer.scheduleAtFixedRate(task,1000,5000);
}
这是完整的代码...
public class Midlet extends MIDlet implements CommandListener{
Display display;
Form fm,ffm;
TextField tf;
Command ok,exit;
StringItem wew;
Image img[];
char cch;
String nia;
Stack s;
Ticker a;
StringItem rep;
Timer timer;
TimerTask timertask;
//private char konia;
char[] chars = {'a', 'b', 'c', 'd', 'e','f','g','h','i','j',
'k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
int weww;
public Midlet()
{
fm=new Form("Name-to-Sign Language");
exit = new Command("EXIT", Command.EXIT, 1);
ok= new Command("OK", Command.STOP, 2);
fm.addCommand(exit);
fm.addCommand(ok);
fm.setCommandListener(this);
wew=new StringItem(null," ");
wew.setLayout(StringItem.LAYOUT_CENTER);
fm.append(wew);
tf=new TextField("Name:","",20,TextField.SENSITIVE);
tf.setLayout(TextField.LAYOUT_CENTER);
fm.append(tf);
ffm=new Form("Translation");
ffm.addCommand(ok);
ffm.setCommandListener(this);
try{
img = new Image[chars.length];
try {
for(int i =0; i<chars.length; i++){
img[i] = Image.createImage("/picture/" + chars[i] + ".JPG");
}
}
catch(Exception error){
error.printStackTrace();
}
}catch(Exception e){}
}
public void startApp() {
if(display==null){
display=Display.getDisplay(this);
}
display.setCurrent(fm);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable d) {
if(c==ok&&d==fm)
{
nia=tf.getString();
int ko=nia.length();
s = new Stack(ko);
for (int i =nia.length()-1; i >=0; i--)
{
cch = nia.charAt(i);
s.push(cch);
}
a=new Ticker(nia+"-to-Sign Language.");
ffm.setTicker(a);
startTimer();
display.setCurrent(ffm);
}
else if(c==ok&&d==ffm)
{
tf.delete(0, nia.length());
timer.cancel();
ffm.deleteAll();
display.setCurrent(fm);
}
else if(c==exit&&d==fm)
{
this.notifyDestroyed();
}
}
最佳答案
您的问题是,每次执行run
时,您都会从堆栈中弹出所有字符,并显示其等效的手语,而没有任何延迟。这意味着您几乎立即到达了最后一个字符。
我认为您应该从while
方法中删除run
循环(也许将while
更改为if
),以便每次计时器运行时,只显示一个字母。
顺便说一下,您可以对此代码进行许多其他简化。