| 14 | */ |
| 15 | public class KeyDemo { |
| 16 | public static void main(String[] args) { |
| 17 | // ����������� |
| 18 | final Frame f = new Frame("�����¼�"); |
| 19 | // �������� |
| 20 | f.setBounds(500, 250, 400, 300); |
| 21 | // ���� |
| 22 | f.setLayout(new FlowLayout()); |
| 23 | |
| 24 | // ������ǩ���� |
| 25 | Label label = new Label("���������qq���룬ֻ����������,����������:"); |
| 26 | // �����ı��� |
| 27 | TextField tf = new TextField(20); |
| 28 | |
| 29 | // ���ӵ����� |
| 30 | f.add(label); |
| 31 | f.add(tf); |
| 32 | |
| 33 | // ע���¼� |
| 34 | f.addWindowListener(new WindowAdapter() { |
| 35 | @Override |
| 36 | public void windowClosing(WindowEvent e) { |
| 37 | System.exit(0); |
| 38 | } |
| 39 | }); |
| 40 | |
| 41 | tf.addKeyListener(new KeyAdapter() { |
| 42 | @Override |
| 43 | public void keyPressed(KeyEvent e) { |
| 44 | // public char getKeyChar() |
| 45 | char ch = e.getKeyChar(); |
| 46 | if (!(ch >= KeyEvent.VK_0 && ch <= KeyEvent.VK_9)) { |
| 47 | System.out.println("��������ǣ�" + ch); |
| 48 | e.consume(); |
| 49 | } |
| 50 | } |
| 51 | }); |
| 52 | |
| 53 | // ���ÿɼ� |
| 54 | f.setVisible(true); |
| 55 | } |
| 56 | } |