3

NetBeansを使用してJavaアプリケーションを開発しています。私は5JTextFieldsと2JTextAreaを持っていJFrameます。ループを使って一度にクリアしたい。どのようにそれを行うことができますか?

4

2 に答える 2

6

すべてのコンポーネントを繰り返し処理し、すべてのオブジェクトJTextFieldJTextAreaオブジェクトのテキストを空の文字列に設定します。

//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
    }
}
于 2012-10-27T06:07:14.117 に答える
2

適切なコードはこれである必要があります

    Component[] components = jframe.getContentPane().getComponents();
    for (Component component : components) {
        if (component instanceof JTextField || component instanceof JTextArea) {
            JTextComponent specificObject = (JTextComponent) component;
            specificObject.setText("");
        }
    }
于 2014-12-12T04:11:01.770 に答える