⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 resettextfieldkeylistener.java

📁 Java游戏高级编程!!很不错的!!!Java游戏高级编程!!很不错的
💻 JAVA
字号:
     package com.croftsoft.core.gui;

     import java.awt.*;
     import java.awt.event.*;

     import com.croftsoft.core.lang.NullArgumentException;

     /*********************************************************************
     * Used to reset a text field upon key press after user input error.
     *
     * <p>
     * Example:
     * <pre>
     * String  text = textField.getText ( );
     *
     * if ( "".equals ( text.trim ( ) ) )
     * {
     *   textField.setBackground ( Color.red );
     *
     *   urlTextField.setText ( "data required" );
     *
     *   textField.addKeyListener (
     *     new ResetTextFieldKeyListener ( textField, Color.white ) );
     * }
     * </pre>
     * </p>
     *
     * <p>
     * Upon key press detection, ResetTextFieldKeyListener will
     * <ol>
     * <li> remove itself as a KeyListener from the TextField,
     * <li> set the TextField background to backgroundColor, and
     * <li> set the TextField to defaultText.
     * </ol>
     * </p>
     *
     * @version
     *   2001-03-23
     * @since
     *   2001-03-23
     * @author
     *   <A HREF="http://www.alumni.caltech.edu/~croft">David W. Croft</A>
     *********************************************************************/

     public final class  ResetTextFieldKeyListener
       extends KeyAdapter
     //////////////////////////////////////////////////////////////////////
     //////////////////////////////////////////////////////////////////////
     {

     private TextField  textField;

     private Color      backgroundColor;

     private String     defaultText;

     //////////////////////////////////////////////////////////////////////
     //////////////////////////////////////////////////////////////////////

     public  ResetTextFieldKeyListener (
       TextField  textField,
       Color      backgroundColor,
       String     defaultText )
     //////////////////////////////////////////////////////////////////////
     {
       NullArgumentException.check ( this.textField = textField );

       NullArgumentException.check (
         this.backgroundColor = backgroundColor );

       NullArgumentException.check ( this.defaultText = defaultText );
     }

     /*********************************************************************
     * this ( textField, backgroundColor, "" );
     *********************************************************************/
     public  ResetTextFieldKeyListener (
       TextField  textField,
       Color      backgroundColor )
     //////////////////////////////////////////////////////////////////////
     {
       this ( textField, backgroundColor, "" );
     }

     //////////////////////////////////////////////////////////////////////
     //////////////////////////////////////////////////////////////////////

     public void  keyPressed ( KeyEvent  keyEvent )
     //////////////////////////////////////////////////////////////////////
     {
       textField.removeKeyListener ( this );

       textField.setBackground ( backgroundColor );

       textField.setText ( defaultText );
     }

     //////////////////////////////////////////////////////////////////////
     //////////////////////////////////////////////////////////////////////
     }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -