e.consume();//?????怎么没用效啊- public class test5 extends JFrame {
- public test5() {
- JTextField field = new JTextField(10);
- field.addKeyListener(new KeyAdapter() {
- //添加事件监听
- public void keyPressed(KeyEvent e)
- {
- System.out.println(e.getKeyCode());
- if(!(e.getKeyCode()>=KeyEvent.VK_0 && e.getKeyCode()<=KeyEvent.VK_9))
- e.consume();//?????怎么没用效啊
- }
- });
-
- this.add(field);
- this.setLayout(new FlowLayout());
- this.setBounds(100, 200, 300, 100);
- this.setVisible(true);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- public static void main(String[] args)
- {
- new test5();
- }
- }
复制代码 仍然可以加入字符
|