Java:如何执行“onclick”对于文本字段?

发布于 2024-10-05 00:32:15 字数 34 浏览 3 评论 0原文

我想让我的文本字段在有人单击它时清除文本。我该怎么做?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱,才寂寞 2024-10-12 00:32:15

java.awt.TextField 上,您可以像这样添加 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 的子类)。 Component 类有一个 addMouseListener() 方法。

或者,您可以将 MouseListener 替换为 java.awt.event.MouseAdapter,它封装了所有 MouseListenerMouseWheelListener和 MouseMotionListener 方法。

来自 JavaDoc(MouseAdapter):

抽象适配器类
接收鼠标事件。中的方法
这个类是空的。本班
存在是为了方便创建
侦听器对象。

鼠标事件可让您跟踪
鼠标按下、释放、单击,
移动、拖动,当它进入
组件,当它退出时以及当
鼠标滚轮已移动。

on java.awt.TextField you can add a MouseListener like so

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) {

    }

});

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.

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

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.

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.

素罗衫 2024-10-12 00:32:15

也许,您需要addMouseListener()

更新
它将像

TextField a = ...;
  a.addMouseListener(new MouseAdapter(){
   public void mouseReleased(MouseEvent e) {
    //some stuff
   }
});

upd2 一样 smt
将 keyListener 修复为 MouseListener

Probably, you need addMouseListener().

upd
It would be smt like

TextField a = ...;
  a.addMouseListener(new MouseAdapter(){
   public void mouseReleased(MouseEvent e) {
    //some stuff
   }
});

upd2
fix keyListener to MouseListener

忆离笙 2024-10-12 00:32:15

试试这个:

  TextField.setText("defaultText");

    TextField.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            reSet(evt);
        }
    });

  TextField.addFocusListener(new java.awt.event.FocusAdapter() {
        public void focusGained(java.awt.event.FocusEvent evt) {
            reSet(evt);
        }
  });

  void reSet(java.awt.event.KeyEvent evt) {
      String temp = jTextField1.getText();
      TextField.setText(temp.equals("defaultText")? "" : temp);
  }

Try this:

  TextField.setText("defaultText");

    TextField.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            reSet(evt);
        }
    });

  TextField.addFocusListener(new java.awt.event.FocusAdapter() {
        public void focusGained(java.awt.event.FocusEvent evt) {
            reSet(evt);
        }
  });

  void reSet(java.awt.event.KeyEvent evt) {
      String temp = jTextField1.getText();
      TextField.setText(temp.equals("defaultText")? "" : temp);
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文