swing 之 jtextfield: 实现Jtextfield的监听[旧作迁移]

方法一:passwardField.addKeyListener(new KeyListener() {
   public void keyPressed(KeyEvent e) {

   }

   public void keyReleased(KeyEvent e) {
    if (passwardField.getText().length() == 0) {
     System.out.println("clear!!!");
     okButton.setEnabled(false);
    }
   }

   public void keyTyped(KeyEvent e) {
    if (passwardField.getText().length() != 0) {
     System.out.println("passwardField");
     okButton.setEnabled(true);
    }

   }

  });



方法二:

passwardField.addCaretListener(new CaretListener() {
 
   public void caretUpdate(CaretEvent e) {
   if (passwardField.getText().length() != 0) {
   System.out.println("passwardField");
   okButton.setEnabled(true);
   } else {
   okButton.setEnabled(false);
   System.out.println("false");
   }
   }
   });

你可能感兴趣的:(swing)