gpt4 book ai didi

Java JTextArea KeyListener

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:07:19 25 4
gpt4 key购买 nike

当我按下 ENTER 时,我的 JTextArea 开始了一个新行,我只想对 doClick() 方法做任何其他事情。我应该怎么做?

textarea.addKeyListener(new KeyListener(){
@Override
public void keyPressed(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_ENTER){
button.doClick();
}
}

@Override
public void keyTyped(KeyEvent e) {
}

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

最佳答案

使用.consume():

Consumes this event so that it will not be processed in the default manner by the source which originated it.

public void keyPressed(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_ENTER){
e.consume();
button.doClick();
}
}

Documentation

关于Java JTextArea KeyListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18483813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com