如何使用循环清除jframe的所有文本字段? [英] How to clear all textfield of jframe using loop?

查看:85
本文介绍了如何使用循环清除jframe的所有文本字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用NetBeans开发Java应用程序.我在JFrame中有5个JTextFields和2个JTextArea.我想使用循环一次清除它们.怎么办?

I'm developing Java application using NetBeans. I have 5 JTextFields and 2 JTextArea in JFrame. I want to clear them at once using a loop. How can it be done?

推荐答案

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

Iterate over all of the components and set the text of all JTextField and JTextArea objects to an empty String:

//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
    }
}

这篇关于如何使用循环清除jframe的所有文本字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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