Java的:我怎么做一个"的onclick"对于文本字段? [英] Java: How do I do a "onclick" for TextField?

查看:123
本文介绍了Java的:我怎么做一个"的onclick"对于文本字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让我的文本字段清除文本当有人点击它。我怎样才能做到这一点?

I want to make my text field clear the text when someone clicks it. How can I do this?

推荐答案

您可以添加的MouseListener 像这样

TextField field = new TextField();
field.addMouseListener(new MouseListener() {

    public void mouseClicked(MouseEvent e) {

    }

    public void mousePressed(MouseEvent e) {

    }

    public void mouseReleased(MouseEvent e) {

    }

    public void mouseEntered(MouseEvent e) {

    }

    public void mouseExited(MouseEvent e) {

    }

});

原因是, java.awt.TextField中的 java.awt.TextComponent中的的一个子类(其中,反过来,是的子类 java.awt.Component中)。在组件类有一个 addMouseListener将()方法。

The reason being that java.awt.TextField is a subclass of java.awt.TextComponent (which, in turn, is a subclass of java.awt.Component). The Component class has a addMouseListener() method.

另外,你可以替换的MouseListener java.awt.event.MouseAdapter 有它封装了所有<$的C $ C>的MouseListener ,的MouseWheelListener 的MouseMotionListener 方法。

Alternatively, you can replace MouseListener with java.awt.event.MouseAdapter has it encapsulates all of MouseListener, MouseWheelListener and MouseMotionListener methods.

从的JavaDoc(的 MouseAdapter

From JavaDoc (of MouseAdapter):

有关的抽象适配器类
  接收鼠标事件。中的方法
  这个类是空的。这个类
  存在是方便创建
  监听的对象。

An abstract adapter class for receiving mouse events. The methods in this class are empty. This class exists as convenience for creating listener objects.

鼠标事件让您追踪时,
  鼠标pssed $ P $,发布,点击,
  移动,拖,当它进入
  元件,当它离开并且当
  鼠标滚轮移动。

Mouse events let you track when a mouse is pressed, released, clicked, moved, dragged, when it enters a component, when it exits and when a mouse wheel is moved.

这篇关于Java的:我怎么做一个&QUOT;的onclick&QUOT;对于文本字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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