黑马程序员技术交流社区
标题:
鼠标拖动选择会激发KeyListener事件????
[打印本页]
作者:
小权
时间:
2013-6-18 14:59
标题:
鼠标拖动选择会激发KeyListener事件????
以下程序在,鼠标在文本框中拖动时激发了KeyListener事件,为什么?
public class JTextFieldtest5 extends JFrame {
public JTextFieldtest5() {
final JTextField field = new JTextField(10);
field.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e)
{
String str = "0123456789.";// 在这里不对小数点进行屏蔽,在点击提交按钮时,若不能转化成数字则给提示
char ch =e.getKeyChar();
if (str.indexOf(ch) == -1)
{
e.consume();
JOptionPane.showMessageDialog(JTextFieldtest5.this, "数据不合法");
}
}
});
this.add(field);
this.add(new JLabel("用鼠标拖拽选择"));
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 JTextFieldtest5();
}
}
复制代码
图片如下:
C:\Users\小权\Desktop\s.png
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2