1
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.");
}

我正在使用上面的代码Button使用 json 创建动态。我可以在控制台中获取值,但无法ButtonForm

4

2 回答 2

1

这显然是对状态机(不是 1.4 BTW 的一部分)的错误使用,因为基类将自行创建和显示表单并设置自己的主题(覆盖您之前所做的任何事情)。

您还忽略了包含不会在设备上运行的创建/定义con1和包含的调用。UnsupportedOperationException

您所有的 LWUIT 代码都应该在状态机中,请按照包括 t-zone 演示在内的演示进行操作,我们会在其中动态替换标题以创建动画。所有这些代码都完全在状态机中,我们在其中覆盖特定的表单初始化方法并从那里修改表单。

如果您想手动编写所有内容,请不要使用状态机,直接使用 UIBuilder。

于 2011-07-28T04:41:38.227 回答
0

我认为您应该在构建按钮之后,即使在显示之后仍然可以将它们添加到表单中,在表单上执行 revalidate()

frm.revalidate();
于 2011-07-30T21:49:31.280 回答