如何使用循环清除jframe的所有textfield?

Pra*_*uja 3 java swing jframe jtextfield jtextarea

我正在使用NetBeans开发Java应用程序.我有5个JTextFields和2个JTextArea在JFrame.我想使用循环立即清除它们.怎么做到呢?

App*_*ish 6

遍历所有的组件,并设置所有的文本JTextField和JTextArea对象为空字符串:

//Note: "this" should be the Container that directly contains your components
//(most likely a JPanel).
//This won't work if you call getComponents on the top-level frame.
for (Component C : this.getComponents())
{    
    if (C instanceof JTextField || C instanceof JTextArea){

        ((JTextComponent) C).setText(""); //abstract superclass
    }
}
Run Code Online (Sandbox Code Playgroud)