我的部分代码如下:
import java.awt.*;
import java.awt.event.*;
class MouseAndKeyEvent1
{
private Frame f;
private Button but;
private TextField tf;
MouseAndKeyEvent1()
{
init();
}
public void init()
{
f= new Frame("my frame");
f.setBounds(300,200,400,400);
f.setLayout(new FlowLayout());
tf = new TextField(20);
but = new Button("haha");
f.add(but);
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+"...是非法的");
}
}
});
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public static void main(String[] args)
{
new MouseAndKeyEvent1();
}
}
自己是在是找不到问题在哪~ |