如何让JTextField响应回车键 [英] How to get a JTextField to respond to the enter key

查看:160
本文介绍了如何让JTextField响应回车键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我希望得到一个JTexField,当按下回车键并将光标放入其中时,将文本放入JTextArea中。任何人都可以帮忙吗?

So I want to get a JTexField to put the text in it into a JTextArea when the enter key is pressed with the cursor in it. Can anyone help?

推荐答案

忘记使用 KeyListener 获取 Swing 组件。

此侦听器设计用于 AWT 组件不为 JTextComponents 。

This listener was designed for use with AWT components does not provide a reliable interaction mechanism for JTextComponents.

使用 ActionListener 代替 - 绝大多数系统上 ActionEvent JTextField 将调度c $ c>。

Use an ActionListener instead - on the vast majority of systems an ActionEvent is dispatched by the JTextField when enter is pressed.

myTextField.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
       myTextArea.append(myTextField.getText() + "\n");
    }
});

这篇关于如何让JTextField响应回车键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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