当前位置: 代码迷 >> J2SE >> java 禁用按键的疑难有关问题
  详细解决方案

java 禁用按键的疑难有关问题

热度:214   发布时间:2016-04-24 12:37:08.0
java 禁用按键的疑难问题
现在我定义了一个JEditorPane
请问如何仅仅只在这个组件上禁用除了上,下,左,右之外的按键呢
不是全局的啊,是仅对改组件有效的

另外请附上一个实例,不要说些无稽之谈的话

谢谢大家了,感激不尽

------解决方案--------------------
Java code
import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import javax.swing.JEditorPane;import javax.swing.JFrame;public class A extends JFrame {    public static void main(String[] args) {                A a = new A();                a.setVisible(true);                JEditorPane p = new JEditorPane();        a.add(p);                p.addKeyListener(new KeyListener() {            @Override            public void keyPressed(KeyEvent arg0) {                int kc = arg0.getKeyCode();                if (kc == KeyEvent.VK_UP ||                         kc == KeyEvent.VK_DOWN||                         kc == KeyEvent.VK_LEFT||                         kc ==KeyEvent.VK_RIGHT)                {                    arg0.consume();                }                            }            @Override            public void keyReleased(KeyEvent arg0) {                int kc = arg0.getKeyCode();                if (kc == KeyEvent.VK_UP ||                         kc == KeyEvent.VK_DOWN||                         kc == KeyEvent.VK_LEFT||                         kc ==KeyEvent.VK_RIGHT)                {                    arg0.consume();                }                            }            @Override            public void keyTyped(KeyEvent arg0) {                int kc = arg0.getKeyCode();                if (kc == KeyEvent.VK_UP ||                         kc == KeyEvent.VK_DOWN||                         kc == KeyEvent.VK_LEFT||                         kc ==KeyEvent.VK_RIGHT)                {                    arg0.consume();                }                            }                    });            }}
  相关解决方案