如何按下按钮清除 jtextfield
我有一个 JButton,当我按下它时,它必须清除 JTextfield 中的所有内容。我该怎么做?也许存在清除 JTextfield 的方法,或者我是否必须将该字段设置为 null
?
I have a JButton and when I push it, it has to clear all the content in a JTextfield. How can I do this? Maybe there exists a method to clear a JTextfield or do I have to set the field to null
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
< code>JTextField#setText 方法应该使用空的
String
作为文本。至于对
JButton
单击做出反应,请为其附加一个ActionListener
。 Swing 教程非常详细地说明了这一切。The
JTextField#setText
method should do it with an emptyString
as text.As for reacting on the
JButton
click, attach anActionListener
to it. The Swing tutorials illustrate this all in great detail.尝试使用
setText(String)
方法,带有空字符串参数:Try using the
setText(String)
method with an empty string argument:您可以调用
JTextField
类的setText("")
方法,并传入一个空字符串作为参数。这会将JTextField
中的内容替换为“”http://docs.oracle.com/javase/7/docs/api/javax/swing/text/JTextComponent.html#setText(java.lang.String )
You can call the
setText("")
method of theJTextField
class passing in an empty string as the parameter. This will replace what is in theJTextField
with ""http://docs.oracle.com/javase/7/docs/api/javax/swing/text/JTextComponent.html#setText(java.lang.String)
您甚至可以扩展
JTextField
类并添加一个名为clear()
的方法。该类看起来像这样:You could even extend the
JTextField
class and add a method calledclear()
. That class would look something like this: