求教TextField与JTextField的区别?
import java.awt.*;import javax.swing.*;
import java.awt.event.*;
class test
{
private Frame f;
private TextField tf;
test()
{
init();
}
public void init()
{
f = new Frame("my frame");
f.setBounds(300,100,600,500);
f.setLayout(new FlowLayout());
tf = new TextField(20);
f.add(tf);
myEvent();
f.setVisible(true);
}
private void myEvent()
{
tf.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
int code = e.getKeyCode();
if(!(code>=KeyEvent.VK_0 && code<=KeyEvent.VK_9))
{
System.out.println(code+".....是非法的");
e.consume();
}
}
});
}
public static void main(String[] args)
{
new test();
}
}
//这是个只能输入数字不能输入字母的代码,但是上面的TextFiel改成JTextField后,字母就能输入了,也就是说监听无效了,为什么?