Java:如何执行“onclick”对于文本字段?
我想让我的文本字段在有人单击它时清除文本。我该怎么做?
I want to make my text field clear the text when someone clicks it. How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在
java.awt.TextField
上,您可以像这样添加MouseListener
原因是
java.awt.TextField
是的子类java.awt.TextComponent
(它又是java.awt.Component
的子类)。Component
类有一个addMouseListener()
方法。或者,您可以将
MouseListener
替换为java.awt.event.MouseAdapter
,它封装了所有MouseListener
、MouseWheelListener
和 MouseMotionListener 方法。来自 JavaDoc(
MouseAdapter
):on
java.awt.TextField
you can add aMouseListener
like soThe reason being that
java.awt.TextField
is a subclass ofjava.awt.TextComponent
(which, in turn, is a subclass ofjava.awt.Component
). TheComponent
class has aaddMouseListener()
method.Alternatively, you can replace
MouseListener
withjava.awt.event.MouseAdapter
has it encapsulates all ofMouseListener
,MouseWheelListener
andMouseMotionListener
methods.From JavaDoc (of
MouseAdapter
):也许,您需要
addMouseListener()
。更新
它将像
upd2 一样 smt
将 keyListener 修复为 MouseListener
Probably, you need
addMouseListener()
.upd
It would be smt like
upd2
fix keyListener to MouseListener
试试这个:
Try this: