简体   繁体   English

KeyEvent不适用于JTextArea,但适用于包含JTextArea的JFrame

[英]KeyEvent not functioning for JTextArea, but works for the JFrame containing the JTextArea

I searched for answers, but all i found is workaounds, not the reason, so i am asking this question: 我搜索了答案,但是我发现的只是解决方法,而不是原因,所以我问这个问题:

I am new to GUI programming. 我是GUI编程的新手。 While practicing some code regarding key event handling, i came accross an example that has a JTextArea contained inside a JFrame. 在练习一些有关键事件处理的代码时,我遇到了一个JFrame内包含JTextArea的示例。 The keylistener interface is implemented by the Frame itself. 侦听器接口由框架本身实现。 When a key is pressed some relevant information are shown on the text area based on the key pressed. 当按下某个键时,基于按下的键,一些相关信息会显示在文本区域中。 the code works fine. 该代码工作正常。

but i tried to go differently and tried to register the JTextarea to a keyListenr instead of the JFrame. 但我尝试另辟and径,并尝试将JTextarea注册到keyListenr而不是JFrame。 However, this does not respond to key events. 但是,这不会响应关键事件。 Here is the code below. 这是下面的代码。 Please help. 请帮忙。

public class KeyDemoFrame extends JFrame
{
private String line1 = "";
private String line2 = "";
private String line3 = "";
private JTextArea textArea;

public KeyDemoFrame()
{
    super("Demonstrating Keystrong events");
    textArea = new JTextArea(10,21);
    textArea.setText("Press any key on keyboard");
    textArea.setEnabled(false);
    textArea.setDisabledTextColor(Color.BLACK);
    add(textArea);
    //addKeyListener( this );

    KeyEventHandler keyStrokeHandler = new KeyEventHandler();
    addKeyListener(keyStrokeHandler);
}   
    private class KeyEventHandler implements KeyListener
    {
        public void keyPressed(KeyEvent event)
        {
            line1 = String.format("Your pressed the %s key", KeyEvent.getKeyText(event.getKeyCode()));
            setLines2and3(event);

        }
        public void keyReleased(KeyEvent event)
        {
            line1 = String.format("You released %s key", KeyEvent.getKeyText(event.getKeyCode()));
            setLines2and3(event);
        }
        public void keyTyped(KeyEvent event)
        {
            line1 = String.format("You typed %s key",KeyEvent.getKeyText(event.getKeyCode()));
            setLines2and3(event);
        }
        private void setLines2and3(KeyEvent event)
        {
            line2 = String.format("This key is %san action key", (event.isActionKey()?"":"not "));
            String temp = KeyEvent.getKeyModifiersText( event.getModifiers() );
            line3 = String.format( "Modifier keys pressed: %s",( temp.equals( "" ) ? "none" : temp ) );
            textArea.setText( String.format( "%s\n%s\n%s\n",line1, line2, line3 ) );
        }
    }

} }

import javax.swing.JFrame;

public class KeyDemo
{
public static void main(String[] args)
{
    KeyDemoFrame keyDemoFrame = new KeyDemoFrame();
    keyDemoFrame.setSize(300, 100);
    keyDemoFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    keyDemoFrame.setVisible(true);
}
}

The reason for this behavior is that the JTextComponent handles the KeyEvent . 出现这种情况的原因是JTextComponent处理KeyEvent As mKorbel already indicated you should use a DocumentListener for JTextComponent s 正如mKorbel已经指出的那样,您应该对JTextComponent使用DocumentListener

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM