求指点,什么打什么都是非法啊
import java.awt.*;
import java.awt.event.*;
class KeyDemo
{
KeyDemo()
{
myWin();
}
private Frame f ;
private Button b ;
private TextField tf;
public void myWin()
{
f = new Frame("my frame");
b = new Button("my key");
tf = new TextField(20);
f.setBounds(300,200,500,400);
f.setLayout(new FlowLayout());
f.add(b);
f.add(tf);
init();
f.setVisible(true);
}
public void init()
{
tf.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
int code = e.getKeyCode();
if(!(code <= KeyEvent.VK_9 && code >= KeyEvent.VK_0))
{
System.out.println("非法输入");
e.consume();
}
}
});
}
public static void main(String[] args)
{
new KeyDemo();
}
}