黑马程序员技术交流社区

标题: 鼠标拖动选择会激发KeyListener事件???? [打印本页]

作者: 小权    时间: 2013-6-18 14:59
标题: 鼠标拖动选择会激发KeyListener事件????
以下程序在,鼠标在文本框中拖动时激发了KeyListener事件,为什么?
  1. public class JTextFieldtest5 extends JFrame {

  2.         public JTextFieldtest5() {
  3.                 final JTextField field = new JTextField(10);
  4.                 field.addKeyListener(new KeyAdapter() {
  5.                         public void keyTyped(KeyEvent e)
  6.                         {
  7.                                 String str = "0123456789.";// 在这里不对小数点进行屏蔽,在点击提交按钮时,若不能转化成数字则给提示
  8.                                 char ch =e.getKeyChar();
  9.                                 if (str.indexOf(ch) == -1)
  10.                                 {
  11.                                         e.consume();
  12.                                         JOptionPane.showMessageDialog(JTextFieldtest5.this, "数据不合法");
  13.                                 }

  14.                         }
  15.                 });

  16.                 this.add(field);
  17.                 this.add(new JLabel("用鼠标拖拽选择"));
  18.                 this.setLayout(new FlowLayout());
  19.                 this.setBounds(100, 200, 300, 100);
  20.                 this.setVisible(true);
  21.                 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  22.         }

  23.         public static void main(String[] args)
  24.         {
  25.                 new JTextFieldtest5();
  26.         }

  27. }
复制代码
图片如下:
C:\Users\小权\Desktop\s.png





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2