按下输入按钮时删除新行功能Java [英] Removing new line function when enter button is pressed Java

查看:110
本文介绍了按下输入按钮时删除新行功能Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本区域,我希望在按下回车按钮时变为空白。我知道这通常会用setText方法完成。但是,当我这样做时,文本被删除,但按下返回键创建的新行功能。我的问题是,无论如何阻止这种默认行为发生?

I have a text area that I would like to become blank when the enter button is pressed. I know this would normally be done with a setText method. However when I do this, the text is removed but the new line function created by the return key being pressed. My question is, is the anyway of stopping this default action from happening?

谢谢

thanks

推荐答案

您是否正在听取文本区域上的ENTER键然后清除它?以下作品适用于我:

Are you listening for the ENTER key on the text area and then clearing it? The following works for me:

final JTextArea ta = new JTextArea();
ta.addKeyListener(new KeyListener() {

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyReleased(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_ENTER){
            ta.setText("");
        }
    }

    @Override
    public void keyPressed(KeyEvent e) {
    }
});

这篇关于按下输入按钮时删除新行功能Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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