如何检测LWUIT形式的按键事件?
我用 LWUIT 包编写了简单的 j2me 程序。我在 MIDLET 类文件中添加了一个 Form
。假设用户按下一个键,然后我想显示另一个 Form
。但我无法在 LWUIT Form
中捕获按键事件。
这是我的代码片段
import javax.microedition.midlet.*;
import com.sun.lwuit.*;
import com.sun.lwuit.events.*;
public class MultipleForm extends MIDlet implements ActionListener{
private Form mFirstForm, mSecondForm;
public void startApp()
{
if (mFirstForm == null)
{
Display.init(this);
mFirstForm = new Form("First Form");
Button button = new Button("Switch");
button.addActionListener(this);
mFirstForm.addComponent(button);
mSecondForm = new Form("Second Form");
Button button2 = new Button("Switch");
button2.addActionListener(this);
mSecondForm.addComponent(button2);
mFirstForm.show();
}
}
protected void keyPressed(int key)
{
System.out.println("Key Pressed");
if(key==52)
{
Form current = Display.getInstance().getCurrent();
if (current == mFirstForm)
{
mSecondForm.show();
}
else if(current==mSecondForm)
{
mFirstForm.show();
}
}
}
public void pauseApp() {}
public void destroyApp(boolean unconditional) {}
}
I have written simple j2me program with LWUIT package.I have added one Form
in my MIDLET class file. Suppose,user press a key then I want to show another Form
.But I couldn't be able to capture key event in my LWUIT Form
.
This is my code snippt
import javax.microedition.midlet.*;
import com.sun.lwuit.*;
import com.sun.lwuit.events.*;
public class MultipleForm extends MIDlet implements ActionListener{
private Form mFirstForm, mSecondForm;
public void startApp()
{
if (mFirstForm == null)
{
Display.init(this);
mFirstForm = new Form("First Form");
Button button = new Button("Switch");
button.addActionListener(this);
mFirstForm.addComponent(button);
mSecondForm = new Form("Second Form");
Button button2 = new Button("Switch");
button2.addActionListener(this);
mSecondForm.addComponent(button2);
mFirstForm.show();
}
}
protected void keyPressed(int key)
{
System.out.println("Key Pressed");
if(key==52)
{
Form current = Display.getInstance().getCurrent();
if (current == mFirstForm)
{
mSecondForm.show();
}
else if(current==mSecondForm)
{
mFirstForm.show();
}
}
}
public void pauseApp() {}
public void destroyApp(boolean unconditional) {}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要捕获 LWUIT
Form
中的事件键,您需要使用Form.addGameKeyListener(这里是键,这里是 actionListener)
键是使用
Canvas
映射的> 例如Canvas.FIRE
。尝试这样做。
To capture the event key in a LWUIT
Form
you need to useForm.addGameKeyListener(here the key, here actionListener)
The keys are mapped using
Canvas
likeCanvas.FIRE
for example.Try to do that.