使用 lwuit 1.4 为表单创建动态按钮
public class StateMachine extends StateMachineBase {
public Container con1;
protected void beforeMainForm(Form f) {
con1 = findMenucon(f);<Br>
super.beforeMainForm(f);<br>
}
}
//class mainmidlet()
public void run() {
try {
//new StateMachine("/App.res");
new mainform("/App.res");
} catch (Exception ex) {
ex.printStackTrace();
}
}
class mainform implements ActionListener{
Vector bname;
Button[] b;
String mainmenu=null;
Form frm;
mainform(String string) {
try {
Resources res = Resources.open(string);
UIManager.getInstance().setThemeProps(res.getTheme(res.getThemeResourceNames()[0]));
UIBuilder builder = new UIBuilder();
frm = (Form)builder.createContainer(res, "MainForm");
StateMachine sm=new StateMachine("/App.res");
System.out.println("------->>>");
bname=new Vector();
this.readmenu();
b = new Button[bname.size()];
System.out.println(b.length+bname.toString());
for (int i = 0; i<b.length; i++) {
b[i] = new Button(bname.elementAt(i).toString());
b[i].setAlignment(Label.CENTER);
b[i].getStyle().setMargin(2,5,5,5);
b[i].getStyle().setPadding(5,5,5,5);
System.out.println(b[i].toString());
b[i].addActionListener(this);
sm.con1.addComponent(b[i]);
//System.out.println("\n " + b[i]);
}
frm.addComponent(sm.con1);
frm.show();
}
catch(IOException err) {
err.printStackTrace();
}
public void actionPerformed(ActionEvent ae) {
throw new UnsupportedOperationException("Not supported yet.");
}
我使用上面的代码使用 json 创建动态 Button
。我可以在控制台中获取值,但无法获取 Form
中的 Button
public class StateMachine extends StateMachineBase {
public Container con1;
protected void beforeMainForm(Form f) {
con1 = findMenucon(f);<Br>
super.beforeMainForm(f);<br>
}
}
//class mainmidlet()
public void run() {
try {
//new StateMachine("/App.res");
new mainform("/App.res");
} catch (Exception ex) {
ex.printStackTrace();
}
}
class mainform implements ActionListener{
Vector bname;
Button[] b;
String mainmenu=null;
Form frm;
mainform(String string) {
try {
Resources res = Resources.open(string);
UIManager.getInstance().setThemeProps(res.getTheme(res.getThemeResourceNames()[0]));
UIBuilder builder = new UIBuilder();
frm = (Form)builder.createContainer(res, "MainForm");
StateMachine sm=new StateMachine("/App.res");
System.out.println("------->>>");
bname=new Vector();
this.readmenu();
b = new Button[bname.size()];
System.out.println(b.length+bname.toString());
for (int i = 0; i<b.length; i++) {
b[i] = new Button(bname.elementAt(i).toString());
b[i].setAlignment(Label.CENTER);
b[i].getStyle().setMargin(2,5,5,5);
b[i].getStyle().setPadding(5,5,5,5);
System.out.println(b[i].toString());
b[i].addActionListener(this);
sm.con1.addComponent(b[i]);
//System.out.println("\n " + b[i]);
}
frm.addComponent(sm.con1);
frm.show();
}
catch(IOException err) {
err.printStackTrace();
}
public void actionPerformed(ActionEvent ae) {
throw new UnsupportedOperationException("Not supported yet.");
}
I am using the above code for creation dynamic Button
using json. i can get the value in console but could not get the Button
in the Form
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这显然是对状态机的错误使用(顺便说一句,这不是 1.4 的一部分),因为基类将自己创建并显示一个表单并设置自己的主题(覆盖您之前所做的任何操作)。
您还忽略了
con1
的创建/定义,并包含了对无法在设备上运行的UnsupportedOperationException
的调用。您的所有 LWUIT 代码都应位于状态机中,请按照包括 t-zone 演示在内的演示进行操作,在该演示中我们会动态替换标题以创建动画。所有这些代码完全位于状态机中,我们在状态机中重写特定的表单初始化方法并从那里修改表单。
如果你想手动编写所有内容,请不要使用状态机,只需直接使用 UIBuilder 即可。
That's clearly incorrect use of the state machine (which isn't a part of 1.4 BTW) since the base class will create and show a form on its own and set its own theme (overriding whatever you did before).
You also neglected to include the creation/definition of
con1
and included calls toUnsupportedOperationException
which won't run on the device.All your LWUIT code should be in the state machine, please follow the demos including the t-zone demo where we replace the title on the fly to create an animation. All of that code is entirely in the state machine where we override the specific form initialization methods and modify the form from there.
If you want to write everything manually don't use the state machine, just use UIBuilder directly.
我认为你应该在构建按钮之后,即使在显示之后,你仍然可以将它们添加到表单中,在表单上执行 revalidate()
I think you should after constructing your Buttons, you can still add them to the Form even after show, the do a revalidate() on the Form