还有,怎么判断jtextfield 里的文本 是否是数字?

解决方案 »

  1.   

    判断数字你可以getKeyChar(),看其ASCII码是否在0-9之间。
    判断backspace你也用getKeyChar()试一试吧。
      

  2.   

    import java.awt.*;
    import java.awt.event.*;
    public class TestwindowListener{
    public static void main(String args[]){
    Frame f = new Frame("Test");
    Button btn1 = new Button("press me");
    Button btn2 = new Button("press here");
    TextArea ta = new TextArea();
    Monitor bh = new Monitor();
    monitor1 kl = new monitor1();

    btn2.setActionCommand("Game over");
    ta.addKeyListener(kl);

    f.add(btn1,"Center");
    f.add(btn2,"North");
    f.add(ta,"West");
    f.addWindowListener(bh);
    f.pack();
    f.setVisible(true);
    }
    }
    class monitor1 implements KeyListener{
    public void keyTyped(KeyEvent e){
    System.out.println(e.KEY_EVENT_MASK);
    }
    public void keyPressed(KeyEvent e){
    System.out.println(e.KEY_EVENT_MASK);
    }
    public void keyReleased(KeyEvent e){
    System.out.println(e.KEY_EVENT_MASK);
    }
    }
    class Monitor implements WindowListener{
    public void windowActivated(WindowEvent e){
    System.out.println("1");
    System.out.println(KeyEvent.VK_BACK_SPACE);

    }
    public void windowClosed(WindowEvent e){
    System.out.println("2");
    }
    public void windowClosing(WindowEvent e){
    System.exit(0);
    System.out.println("3");
    }
    public void windowDeactivated(WindowEvent e){
    System.out.println("4");
    }
    public void windowDeiconified(WindowEvent e){
    System.out.println("5");
    }
    public void windowIconified(WindowEvent e){
    System.out.println("6");
    }
    public void windowOpened(WindowEvent e){
    System.out.println("7");
    }
    }
      

  3.   

    我要只能敲数字,而且能用 back space 键