在 j2me 中的自定义 GUI 中遍历
这是我的遍历方法代码,
protected boolean traverse(int dir, int viewportWidth, int viewportHeight,
int[] visRect_inout) {
try {
if (hori && vert) {
// CustomItems items=new CustomItems("Hi");
switch (dir) {
case Canvas.DOWN:
this.a=dir; //Canvas.DOWN
this.b=viewportWidth; //b=2
this.c=viewportHeight; //c=3
this.d=visRect_inout; //d={2,2,250,250}
this.traverse(Canvas.UP, b, c, d);
break;
case Canvas.UP:
this.a=dir;
this.j=viewportWidth;
this.k=viewportHeight;
this.d=visRect_inout;
this.traverse(Canvas.UP, j, k, d);
break;
case Canvas.LEFT:
this.a=dir;
this.j=viewportWidth;
this.k=viewportHeight;
this.d=visRect_inout;
this.traverse(Canvas.LEFT, j, k, d);
break;
case Canvas.RIGHT:
break;
}
}
} catch (Exception e) {
System.out.println("Exception " + e);
}
return false;
}
我对自定义项目非常陌生。
如果我做错了什么,请告诉我。
Here is my Traverse method code
protected boolean traverse(int dir, int viewportWidth, int viewportHeight,
int[] visRect_inout) {
try {
if (hori && vert) {
// CustomItems items=new CustomItems("Hi");
switch (dir) {
case Canvas.DOWN:
this.a=dir; //Canvas.DOWN
this.b=viewportWidth; //b=2
this.c=viewportHeight; //c=3
this.d=visRect_inout; //d={2,2,250,250}
this.traverse(Canvas.UP, b, c, d);
break;
case Canvas.UP:
this.a=dir;
this.j=viewportWidth;
this.k=viewportHeight;
this.d=visRect_inout;
this.traverse(Canvas.UP, j, k, d);
break;
case Canvas.LEFT:
this.a=dir;
this.j=viewportWidth;
this.k=viewportHeight;
this.d=visRect_inout;
this.traverse(Canvas.LEFT, j, k, d);
break;
case Canvas.RIGHT:
break;
}
}
} catch (Exception e) {
System.out.println("Exception " + e);
}
return false;
}
I am very new to Custom Item.
If I had done any wrong, let me know.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 switch 语句中调用
traverse
的方式对我来说看起来很棘手:到目前为止您发布的代码相当零散,但据我所知,上面将导致对
的无限递归调用遍历
,最终以堆栈溢出错误结束。在您的特定情况下,这可能是无害的,因为您从方法中无条件返回
false
。根据我的理解,这意味着设备永远不会尝试使用 Canvas UP 和其他潜在危险值调用遍历。请随意检查 lcdui CustomItem#traverse API 文档了解更多详细信息(如果您是) 感兴趣的。另外,如果您使用 Wireless Toolkit / Java ME SDK,您可以查看它们的代码示例。根据我的回忆,那里有 CustomItem 代码的很好的工作示例。
The way how you invoke
traverse
from within switch statement looks slippery to me:The code you posted so far is quite fragmentary but as far as I can tell, above will lead to infinite recursive calls for
traverse
, eventually ending in stack overflow error.In your particular case this might be harmless though because you unconditionally return
false
from your method. Per my understanding this means that device will never attempt to invoke traverse withCanvas UP
and other potentially dangerous values. Feel free to check lcdui CustomItem#traverse API documentation for more details if you're interested.Also, if you are using Wireless Toolkit / Java ME SDK, you could check their code examples. Per my recollection, there were nice working examples of CustomItem code there.