我正在尝试刷新复合中的clabel。但是,clabel并不总是存在。我需要一种方法来检查复合材料中是否存在。我已经尝试了复合上的getChildren类,并且能够使用它来找到复合上的所有CLabel,但是我无法解析它们。

这就是我到目前为止

Control[] childs = comp.getChildren();

for (int i = 0; i < childs.length; i++) {
    if(childs[i].getClass().getSimpleName().equalsIgnoreCase("CLabel")){

    }
}

最佳答案

为什么不使用instanceof然后进行转换?

Control[] children = comp.getChildren();

for (int i = 0; i < children.length; i++)
{
    if(children[i] instanceof CLabel)
    {
        CLabel label = (CLabel) children[i];

        /* Do something with the label */
    }
}

07-26 08:31