gpt4 book ai didi

java - 如何使用循环清除 jframe 的所有文本字段?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:13:31 24 4
gpt4 key购买 nike

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

最佳答案

遍历所有组件并将所有 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
}
}

关于java - 如何使用循环清除 jframe 的所有文本字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13097626/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com