检查java组件是否为空

时间:2016-05-16 13:12:34

标签: java components is-empty

为什么如果没有'检查组件是否等于""

  private boolean ControllaCampi(){
        boolean verifica = true;
        for (Component component : pMain.getComponents()){
        if ((component.getClass().equals(JTextField.class)) ||  component.getClass().equals(JFormattedTextField.class))){
            if (component.equals("")){
                verifica = false;
                break;
            }
        }
    }
    return verifica;
}

2 个答案:

答案 0 :(得分:0)

它不起作用,因为该组件不是String!

要检查组件是否为空,您可以执行以下操作:

if (component.getComponentCount()==0){
    verifica = false;
    break;
}

答案 1 :(得分:0)

您正在将橙子与苹果进行比较。 Component无法与""进行比较,StringString componentText = component.getText(); if (componentText.equals("")) { ... } 个实例。您必须获取文本以检查它是否为空:

component.getClass().equals(...)

此外,您可以将component instanceof JTextField替换为true。它会将JTextField返回JFormattedTextFieldJTextField{ "_id" : "s2QBCnv6fXv5YbjAP", "question" : "Is this real change?", "createdAt" : ISODate("2016-05-13T21:05:23.381Z"), "yes" : [ { "heading" : "Yes It is", "body" : "I think this government knows what they are doing. That's why there has not been any form of protest", "email" : "I think this government knows what they are doing. That's why there has not been any form of protest", "createdAt" : ISODate("2016-05-13T21:08:25.119Z"), "replies" : [ ], "likes" : [ ] }, { "heading" : "Well, Yes", "body" : "I think this is change as we all want to know what the government is doing and I am grateful to be alive at this time", "email" : "I think this is change as we all want to know what the government is doing and I am grateful to be alive at this time", "createdAt" : ISODate("2016-05-13T21:10:47.123Z"), "replies" : [ ], "likes" : [ ] } ], "no" : [ { "heading" : "Not at All", "body" : "This is not the change I wanted. This is waste of four years and I amm waiting to see the promised change", "email" : "kenshin@kay.com", "createdAt" : ISODate("2016-05-13T21:12:58.977Z"), "replies" : [ ], "likes" : [ ] } ], "author" : "admin", "image" : "/cfs/files/QuestionImages/DzdpK6NdurZMTwAse" 的子类)。