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

📄 constrainedtextfield.java

📁 FuncPlotter is a combined Java application and applet for displaying two-dimensional plots of explic
💻 JAVA
字号:
/*====================================================================*\ConstrainedTextField.javaConstrained text field class.------------------------------------------------------------------------This file is part of FuncPlotter, a combined Java application and appletfor plotting explicit functions in one variable.Copyright 2005-2007 Andy Morgan-Richards.FuncPlotter is free software: you can redistribute it and/or modify itunder the terms of the GNU General Public License as published by theFree Software Foundation, either version 3 of the License, or (at youroption) any later version.This program is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public License alongwith this program.  If not, see <http://www.gnu.org/licenses/>.\*====================================================================*/// PACKAGEpackage textfield;//----------------------------------------------------------------------// IMPORTSimport java.awt.Toolkit;import javax.swing.text.AttributeSet;import javax.swing.text.BadLocationException;import javax.swing.text.Document;import javax.swing.text.PlainDocument;//----------------------------------------------------------------------// CONSTRAINED TEXT FIELD CLASSpublic class ConstrainedTextField    extends AbstractTextField{//////////////////////////////////////////////////////////////////////////  Member classes : inner classes////////////////////////////////////////////////////////////////////////    // CONSTRAINED TEXT DOCUMENT CLASS    private class ConstrainedTextDocument        extends PlainDocument    {    ////////////////////////////////////////////////////////////////////    //  Constants    ////////////////////////////////////////////////////////////////////        private static final    String  BEYOND_END_STR  = "Offset beyond end of text";    ////////////////////////////////////////////////////////////////////    //  Constructors    ////////////////////////////////////////////////////////////////////        private ConstrainedTextDocument( )        {        }        //--------------------------------------------------------------    ////////////////////////////////////////////////////////////////////    //  Instance methods : overriding methods    ////////////////////////////////////////////////////////////////////        public void insertString( int          offset,                                  String       str,                                  AttributeSet attrSet )            throws BadLocationException        {            // Test for offset beyond end of text            int currentLength = getLength( );            if ( offset > currentLength )                throw new BadLocationException( BEYOND_END_STR, offset );            // Translate string            String srcStr = translateInsertString( str, offset );            // Create an array of characters to be inserted from the valid characters in the string            char[] insertBuf = new char[srcStr.length( )];            int insertLength = 0;            for ( int i = 0; i < srcStr.length( ); ++i )            {                char ch = validateCharacter( srcStr.charAt( i ), offset + insertLength );                if ( ch != TextFieldConstants.INVALID_CHAR )                    insertBuf[insertLength++] = ch;            }            boolean isError = (insertLength < srcStr.length( ));            // If inserted characters would make the text too long, remove excess from end of text            if ( currentLength + insertLength > maxLength )            {                isError = true;                if ( insertLength > maxLength - offset )                    insertLength = maxLength - offset;                if ( (insertLength > 0) && (offset < currentLength) )                {                    int excessLength = currentLength + insertLength - maxLength;                    if ( excessLength > 0 )                        remove( currentLength - excessLength, excessLength );                }            }            // Beep if not all input string will be inserted            if ( isError )                Toolkit.getDefaultToolkit( ).beep( );            // Insert valid characters and set caret position to end of insertion            if ( insertLength > 0 )            {                super.insertString( offset, new String( insertBuf, 0, insertLength ), attrSet );                setCaretPosition( offset + insertLength );            }        }        //--------------------------------------------------------------    }    //==================================================================//////////////////////////////////////////////////////////////////////////  Constructors////////////////////////////////////////////////////////////////////////    public ConstrainedTextField( int maxLength )    {        super( maxLength );        this.maxLength = maxLength;    }    //------------------------------------------------------------------    public ConstrainedTextField( int    maxLength,                                 String text )    {        this( maxLength );        setText( text );    }    //------------------------------------------------------------------    public ConstrainedTextField( int maxLength,                                 int columns )    {        super( columns );        this.maxLength = (maxLength == 0) ? Integer.MAX_VALUE : maxLength;    }    //------------------------------------------------------------------    public ConstrainedTextField( int    maxLength,                                 int    columns,                                 String text )    {        this( maxLength, columns );        setText( text );    }    //------------------------------------------------------------------//////////////////////////////////////////////////////////////////////////  Instance methods : overriding methods////////////////////////////////////////////////////////////////////////    protected Document createDefaultModel( )    {        return new ConstrainedTextDocument( );    }    //------------------------------------------------------------------//////////////////////////////////////////////////////////////////////////  Instance methods////////////////////////////////////////////////////////////////////////    protected String translateInsertString( String str,                                            int    offset )    {        return str;    }    //------------------------------------------------------------------    protected char validateCharacter( char ch,                                      int  index )    {        return ch;    }    //------------------------------------------------------------------//////////////////////////////////////////////////////////////////////////  Instance variables////////////////////////////////////////////////////////////////////////    private int maxLength;}//----------------------------------------------------------------------

⌨️ 快捷键说明

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