How to use variables to refer to Components in codename one?

和自甴很熟 提交于 2019-12-08 02:28:06

问题


I created a UI with the UIBuilder.

Instead of always using the findXXXX (), I would love to do something like this in the Statemachine:

public class Statemachine ... {

private Container c;
private Tabs t;


initVars () {
   initAll();
}

....
private initAll () {
   Container c = findContainer();
   Tabs t = findTabs();
   ...
}

}

and use them as c.getYYY().

Is this possible? If yes, how?


回答1:


Yes you can do that, but not advisable.

The danger of it is when you have 2 components with the same name on different forms, this may reference wrong component and may be hard to debug.

If you sure can deal with the above, you can declare and initiate the variables inside Statemachine class but outside of all methods as a private global variable.

public class Statemachine ... {

    private Container c = findContainer();
    private Tabs t = findTabs(); 

    initVars () {

    }

    ....
}



回答2:


You can't do that in the current GUI builder since the current GUI builder creates forms dynamically and discards them during navigation.

The new GUI builder uses a completely different architecture and will work exactly like that: 1 class per one UI form. So you will have variables for every field.

You can see this in action here.

Note that at this time the GUI builder is still in pre-beta stage although we are heavily committed to it and its architecture.



来源:https://stackoverflow.com/questions/35057483/how-to-use-variables-to-refer-to-components-in-codename-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!