如何使用变量来引用代号为1的组件? [英] How to use variables to refer to Components in codename one?

查看:43
本文介绍了如何使用变量来引用代号为1的组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用UIBuilder创建了一个UI。

I created a UI with the UIBuilder.

我将不再总是使用 findXXXX()喜欢在 Statemachine 中做这样的事情:

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

}

并将它们用作 c.getYYY()

这可能吗?如果是,怎么办?

Is this possible? If yes, how?

推荐答案

是的,您可以这样做,但不建议这样做。

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.

如果可以解决上述问题,则可以在Statemachine类内部但在所有方法外部将变量声明并初始化为私有全局变量。

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 () {

    }

    ....
}

这篇关于如何使用变量来引用代号为1的组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆