Java JTextArea KeyListener

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18483813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 02:03:19  来源:igfitidea点击:

Java JTextArea KeyListener

javaswinglistenerjlabelkeylistener

提问by PETI258

When I pressed the ENTER my JTextArea starts a new row and I only want do to the doClick() method nothing else. How should I do that?

当我按下 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) {
    }
});

采纳答案by Kevin Bowersox

Use .consume():

使用.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

文档

回答by nIcE cOw

You should use KeyBindingswith any JTextComponentin question. KeyListenersare way too low level from Swing's perspective. You are using the concept which was related to AWT, Swinguses KeyBindingsto do the same task with more efficiency and provides desired results :-)

您应该对任何有问题的人使用KeyBindingsJTextComponentKeyListenersSwing的角度来看,水平太低了。您正在使用与 相关的概念AWTSwing用于KeyBindings以更高的效率执行相同的任务并提供所需的结果:-)

A small program for your help :

一个小程序为您提供帮助:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class KeyBindingExample {

    private static final String key = "ENTER";
    private KeyStroke keyStroke;

    private JButton button;
    private JTextArea textArea;

    private Action wrapper = new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            button.doClick();
        }
    };

    private void displayGUI() {
        JFrame frame = new JFrame("Key Binding Example");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel contentPane = new JPanel(new BorderLayout(5, 5));

        textArea = new JTextArea(10, 10);
        keyStroke = KeyStroke.getKeyStroke(key);
        Object actionKey = textArea.getInputMap(
                JComponent.WHEN_FOCUSED).get(keyStroke);
        textArea.getActionMap().put(actionKey, wrapper);

        button = new JButton("Click Me!");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                System.out.format("Button Clicked :-)%n");
            }
        });     

        contentPane.add(textArea, BorderLayout.CENTER);
        contentPane.add(button, BorderLayout.PAGE_END);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                new KeyBindingExample().displayGUI();
            }
        };
        EventQueue.invokeLater(r);
    }
}