向 JtextArea 添加键侦听器 [英] Adding a Key Listener to a JtextArea

查看:40
本文介绍了向 JtextArea 添加键侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 input 的 JTextArea,当我按下向上箭头键时,我试图将字符串 inputValue 加载到其中.到目前为止,这段代码似乎不起作用,我不确定为什么.请帮忙.

I have a JTextArea called input and I am trying to get the string inputValue loaded into it when I press the up arrow key. so far this code does not seem to work and I am unsure as to why. Please help.

    input.addKeyListener(new KeyListener() {            
        public void keyTyped(KeyEvent e) {
            System.out.println("test");
            if(e.getKeyCode() == KeyEvent.VK_UP) {
                input.setText(inputValue);
                System.out.println("up is pressed");
            }       
        }

        @Override
        public void keyPressed(KeyEvent e) {
            // TODO Auto-generated method stub  
        }

        @Override
        public void keyReleased(KeyEvent e) {
            // TODO Auto-generated method stub      
        }
    });

推荐答案

你应该覆盖 void keypressed 而不是 keytyped

you should override void keypressed instead of keytyped

@Override
        public void keyPressed(KeyEvent e) {
            System.out.println("test");
            if(e.getKeyCode() == KeyEvent.VK_UP) {
                input.setText(inputValue);
                System.out.println("up is pressed");

        }

因为它不是一个角色

这篇关于向 JtextArea 添加键侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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