تقييم الموضوع :
  • 0 أصوات - بمعدل 0
  • 1
  • 2
  • 3
  • 4
  • 5
لجعل مربع النص لا يقبل سوي أرقام
#1
كاتب الموضوع : Friendassist



قمت بارفاق الكود بلغات الدوت نت أيضا حتي تعم الفائدة و نحدد الفروق بين هذه اللغات الاكثر استخداما علي الساحه ..^^
}}}}
تم الشكر بواسطة:
#2
و هذه تعتبر افضل طريقه لجعل مربع النص يستقبل ارقاما فقط في لغة الجافا .. و لن تجدوا افضل و اكثر اختصارا منها علي الانترنت ..
}}}}
تم الشكر بواسطة:
#3
يمكن استخدامها مع المكتبة awt أيضا من خلال مربع النص الخاص بالمكتبة
}}}}
تم الشكر بواسطة:
#4
طبعا هذه هي الطريقه الاخري الاكثر شيوعا علي الانترنت .. و من المستحيل علي شخص عاقل ان يستخدمها ضمانا للحفاظ علي الوقت و المجهود .. ولكن قررت وضعها حتي يستفيد الجميع :

كود :
[color=#000000][COLOR=#0000bb]import javax[/color][color=#007700].[/color][color=#0000bb]swing[/color][COLOR=#007700].*;
[/COLOR][color=#0000bb]import javax[/color][color=#007700].[/color][color=#0000bb]swing[/color][color=#007700].[/color][color=#0000bb]text[/color][COLOR=#007700].*;

public class [/COLOR][color=#0000bb]JNumberTextField [/color][color=#007700]extends [/color][COLOR=#0000bb]JTextField
[/COLOR][COLOR=#007700]{
    private static final [/COLOR][color=#0000bb]char DOT [/color][color=#007700]= [/color][color=#dd0000]'.'[/color][COLOR=#007700];
    private static final [/COLOR][color=#0000bb]char NEGATIVE [/color][color=#007700]= [/color][color=#dd0000]'-'[/color][COLOR=#007700];
    private static final [/COLOR][color=#0000bb]String BLANK [/color][color=#007700]= [/color][color=#dd0000]""[/color][COLOR=#007700];
    private static final [/COLOR][color=#0000bb]int DEF_PRECISION [/color][color=#007700]= [/color][color=#0000bb]2[/color][COLOR=#007700];

    public static final [/COLOR][color=#0000bb]int NUMERIC [/color][color=#007700]= [/color][color=#0000bb]2[/color][COLOR=#007700];
    public static final [/COLOR][color=#0000bb]int DECIMAL [/color][color=#007700]= [/color][color=#0000bb]3[/color][COLOR=#007700];

    public static final [/COLOR][color=#0000bb]String FM_NUMERIC [/color][color=#007700]= [/color][color=#dd0000]"0123456789"[/color][COLOR=#007700];
    public static final [/COLOR][color=#0000bb]String FM_DECIMAL [/color][color=#007700]= [/color][color=#0000bb]FM_NUMERIC [/color][color=#007700]+ [/color][color=#0000bb]DOT[/color][COLOR=#007700];

    private [/COLOR][color=#0000bb]int maxLength [/color][color=#007700]= [/color][color=#0000bb]0[/color][COLOR=#007700];
    private [/COLOR][color=#0000bb]int format [/color][color=#007700]= [/color][color=#0000bb]NUMERIC[/color][COLOR=#007700];
    private [/COLOR][color=#0000bb]String negativeChars [/color][color=#007700]= [/color][color=#0000bb]BLANK[/color][COLOR=#007700];
    private [/COLOR][color=#0000bb]String allowedChars [/color][color=#007700]= [/color][color=#0000bb]null[/color][COLOR=#007700];
    private [/COLOR][color=#0000bb]boolean allowNegative [/color][color=#007700]= [/color][color=#0000bb]false[/color][COLOR=#007700];
    private [/COLOR][color=#0000bb]int precision [/color][color=#007700]= [/color][color=#0000bb]0[/color][COLOR=#007700];

    protected [/COLOR][color=#0000bb]PlainDocument numberFieldFilter[/color][COLOR=#007700];

    public [/COLOR][color=#0000bb]JNumberTextField[/color][COLOR=#007700]()
    {
        [/COLOR][color=#0000bb]this[/color][color=#007700]( [/color][color=#0000bb]10[/color][color=#007700], [/color][color=#0000bb]NUMERIC [/color][COLOR=#007700]);
    }

    public [/COLOR][color=#0000bb]JNumberTextField[/color][color=#007700]( [/color][color=#0000bb]int maxLen [/color][COLOR=#007700])
    {
        [/COLOR][color=#0000bb]this[/color][color=#007700]( [/color][color=#0000bb]maxLen[/color][color=#007700], [/color][color=#0000bb]NUMERIC [/color][COLOR=#007700]);
    }

    public [/COLOR][color=#0000bb]JNumberTextField[/color][color=#007700]( [/color][color=#0000bb]int maxLen[/color][color=#007700], [/color][color=#0000bb]int format [/color][COLOR=#007700])
    {
        [/COLOR][color=#0000bb]setAllowNegative[/color][color=#007700]( [/color][color=#0000bb]true [/color][COLOR=#007700]);
        [/COLOR][color=#0000bb]setMaxLength[/color][color=#007700]( [/color][color=#0000bb]maxLen [/color][COLOR=#007700]);
        [/COLOR][color=#0000bb]setFormat[/color][color=#007700]( [/color][color=#0000bb]format [/color][COLOR=#007700]);

        [/COLOR][color=#0000bb]numberFieldFilter [/color][color=#007700]= new [/color][color=#0000bb]JNumberFieldFilter[/color][COLOR=#007700]();
        [/COLOR][color=#0000bb]super[/color][color=#007700].[/color][color=#0000bb]setDocument[/color][color=#007700]( [/color][color=#0000bb]numberFieldFilter [/color][COLOR=#007700]);
    }

    public [/COLOR][color=#0000bb]void setMaxLength[/color][color=#007700]( [/color][color=#0000bb]int maxLen [/color][COLOR=#007700])
    {
        if ([/COLOR][color=#0000bb]maxLen [/color][color=#007700]> [/color][color=#0000bb]0[/color][COLOR=#007700])
            [/COLOR][color=#0000bb]maxLength [/color][color=#007700]= [/color][color=#0000bb]maxLen[/color][COLOR=#007700];
        else
            [/COLOR][color=#0000bb]maxLength [/color][color=#007700]= [/color][color=#0000bb]0[/color][COLOR=#007700];
    }

    public [/COLOR][color=#0000bb]int getMaxLength[/color][COLOR=#007700]()
    {
        return [/COLOR][color=#0000bb]maxLength[/color][COLOR=#007700];
    }

    public [/COLOR][color=#0000bb]void setPrecision[/color][color=#007700]( [/color][color=#0000bb]int precision [/color][COLOR=#007700])
    {
        if ( [/COLOR][color=#0000bb]format [/color][color=#007700]== [/color][color=#0000bb]NUMERIC [/color][COLOR=#007700])
            return;

        if ( [/COLOR][color=#0000bb]precision [/color][color=#007700]>= [/color][color=#0000bb]0 [/color][COLOR=#007700])
            [/COLOR][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]precision [/color][color=#007700]= [/color][color=#0000bb]precision[/color][COLOR=#007700];
        else
            [/COLOR][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]precision [/color][color=#007700]= [/color][color=#0000bb]DEF_PRECISION[/color][COLOR=#007700];
    }

    public [/COLOR][color=#0000bb]int getPrecision[/color][COLOR=#007700]()
    {
        return [/COLOR][color=#0000bb]precision[/color][COLOR=#007700];
    }

    public [/COLOR][color=#0000bb]Number getNumber[/color][COLOR=#007700]()
    {
        [/COLOR][color=#0000bb]Number number [/color][color=#007700]= [/color][color=#0000bb]null[/color][COLOR=#007700];

        if ( [/COLOR][color=#0000bb]format [/color][color=#007700]== [/color][color=#0000bb]NUMERIC [/color][COLOR=#007700])
            [/COLOR][color=#0000bb]number [/color][color=#007700]= new [/color][color=#0000bb]Integer[/color][color=#007700]([/color][color=#0000bb]getText[/color][COLOR=#007700]());
        else
            [/COLOR][color=#0000bb]number [/color][color=#007700]= new [/color][color=#0000bb]Double[/color][color=#007700]([/color][color=#0000bb]getText[/color][COLOR=#007700]());

        return [/COLOR][color=#0000bb]number[/color][COLOR=#007700];
    }

    public [/COLOR][color=#0000bb]void setNumber[/color][color=#007700]( [/color][color=#0000bb]Number value [/color][COLOR=#007700])
    {
        [/COLOR][color=#0000bb]setText[/color][color=#007700]([/color][color=#0000bb]String[/color][color=#007700].[/color][color=#0000bb]valueOf[/color][color=#007700]([/color][color=#0000bb]value[/color][COLOR=#007700]));
    }

    public [/COLOR][color=#0000bb]int getInt[/color][COLOR=#007700]()
    {
        return [/COLOR][color=#0000bb]Integer[/color][color=#007700].[/color][color=#0000bb]parseInt[/color][color=#007700]( [/color][color=#0000bb]getText[/color][COLOR=#007700]() );
    }

    public [/COLOR][color=#0000bb]void setInt[/color][color=#007700]( [/color][color=#0000bb]int value [/color][COLOR=#007700])
    {
        [/COLOR][color=#0000bb]setText[/color][color=#007700]( [/color][color=#0000bb]String[/color][color=#007700].[/color][color=#0000bb]valueOf[/color][color=#007700]( [/color][color=#0000bb]value [/color][COLOR=#007700]) );
    }

    public [/COLOR][color=#0000bb]float getFloat[/color][COLOR=#007700]()
    {
        return ( new [/COLOR][color=#0000bb]Float[/color][color=#007700]( [/color][color=#0000bb]getText[/color][color=#007700]() ) ).[/color][color=#0000bb]floatValue[/color][COLOR=#007700]();
    }

    public [/COLOR][color=#0000bb]void setFloat[/color][color=#007700]([/color][color=#0000bb]float value[/color][COLOR=#007700])
    {
        [/COLOR][color=#0000bb]setText[/color][color=#007700]( [/color][color=#0000bb]String[/color][color=#007700].[/color][color=#0000bb]valueOf[/color][color=#007700]( [/color][color=#0000bb]value [/color][COLOR=#007700]) );
    }

    public [/COLOR][color=#0000bb]double getDouble[/color][COLOR=#007700]()
    {
        return ( new [/COLOR][color=#0000bb]Double[/color][color=#007700]( [/color][color=#0000bb]getText[/color][color=#007700]() ) ).[/color][color=#0000bb]doubleValue[/color][COLOR=#007700]();
    }

    public [/COLOR][color=#0000bb]void setDouble[/color][color=#007700]([/color][color=#0000bb]double value[/color][COLOR=#007700])
    {
        [/COLOR][color=#0000bb]setText[/color][color=#007700]( [/color][color=#0000bb]String[/color][color=#007700].[/color][color=#0000bb]valueOf[/color][color=#007700]([/color][color=#0000bb]value[/color][COLOR=#007700]) );
    }

    public [/COLOR][color=#0000bb]int getFormat[/color][COLOR=#007700]()
    {
        return [/COLOR][color=#0000bb]format[/color][COLOR=#007700];
    }

    public [/COLOR][color=#0000bb]void setFormat[/color][color=#007700]([/color][color=#0000bb]int format[/color][COLOR=#007700])
    {
        switch ( [/COLOR][color=#0000bb]format [/color][COLOR=#007700])
        {
        case [/COLOR][color=#0000bb]NUMERIC[/color][COLOR=#007700]:
        default:
            [/COLOR][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]format [/color][color=#007700]= [/color][color=#0000bb]NUMERIC[/color][COLOR=#007700];
            [/COLOR][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]precision [/color][color=#007700]= [/color][color=#0000bb]0[/color][COLOR=#007700];
            [/COLOR][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]allowedChars [/color][color=#007700]= [/color][color=#0000bb]FM_NUMERIC[/color][COLOR=#007700];
            break;

        case [/COLOR][color=#0000bb]DECIMAL[/color][COLOR=#007700]:
            [/COLOR][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]format [/color][color=#007700]= [/color][color=#0000bb]DECIMAL[/color][COLOR=#007700];
            [/COLOR][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]precision [/color][color=#007700]= [/color][color=#0000bb]DEF_PRECISION[/color][COLOR=#007700];
            [/COLOR][color=#0000bb]this[/color][color=#007700].[/color][color=#0000bb]allowedChars [/color][color=#007700]= [/color][color=#0000bb]FM_DECIMAL[/color][COLOR=#007700];
            break;
        }
    }

    public [/COLOR][color=#0000bb]void setAllowNegative[/color][color=#007700]( [/color][color=#0000bb]boolean value [/color][COLOR=#007700])
    {
        [/COLOR][color=#0000bb]allowNegative [/color][color=#007700]= [/color][color=#0000bb]value[/color][COLOR=#007700];

        if ( [/COLOR][color=#0000bb]value [/color][COLOR=#007700])
            [/COLOR][color=#0000bb]negativeChars [/color][color=#007700]= [/color][color=#dd0000]"" [/color][color=#007700]+ [/color][color=#0000bb]NEGATIVE[/color][COLOR=#007700];
        else
            [/COLOR][color=#0000bb]negativeChars [/color][color=#007700]= [/color][color=#0000bb]BLANK[/color][COLOR=#007700];
    }

    public [/COLOR][color=#0000bb]boolean isAllowNegative[/color][COLOR=#007700]()
    {
        return [/COLOR][color=#0000bb]allowNegative[/color][COLOR=#007700];
    }

    public [/COLOR][color=#0000bb]void setDocument[/color][color=#007700]( [/color][color=#0000bb]Document document [/color][COLOR=#007700])
    {
    }

    class [/COLOR][color=#0000bb]JNumberFieldFilter [/color][color=#007700]extends [/color][COLOR=#0000bb]PlainDocument
    [/COLOR][COLOR=#007700]{
        public [/COLOR][color=#0000bb]JNumberFieldFilter[/color][COLOR=#007700]()
        {
            [/COLOR][color=#0000bb]super[/color][COLOR=#007700]();
        }

        public [/COLOR][color=#0000bb]void insertString[/color][color=#007700]([/color][color=#0000bb]int offset[/color][color=#007700], [/color][color=#0000bb]String str[/color][color=#007700], [/color][color=#0000bb]AttributeSet attr[/color][color=#007700]) [/color][COLOR=#0000bb]throws BadLocationException
        [/COLOR][COLOR=#007700]{
            [/COLOR][color=#0000bb]String text [/color][color=#007700]= [/color][color=#0000bb]getText[/color][color=#007700]([/color][color=#0000bb]0[/color][color=#007700],[/color][color=#0000bb]offset[/color][color=#007700]) + [/color][color=#0000bb]str [/color][color=#007700]+ [/color][color=#0000bb]getText[/color][color=#007700]([/color][color=#0000bb]offset[/color][color=#007700],([/color][color=#0000bb]getLength[/color][color=#007700]() - [/color][color=#0000bb]offset[/color][COLOR=#007700]));

            if ( [/COLOR][color=#0000bb]str [/color][color=#007700]== [/color][color=#0000bb]null [/color][color=#007700]|| [/color][color=#0000bb]text [/color][color=#007700]== [/color][color=#0000bb]null [/color][COLOR=#007700])
                return;

            for ( [/COLOR][color=#0000bb]int i[/color][color=#007700]=[/color][color=#0000bb]0[/color][color=#007700]; [/color][color=#0000bb]i[/color][color=#007700]<[/color][color=#0000bb]str[/color][color=#007700].[/color][color=#0000bb]length[/color][color=#007700](); [/color][color=#0000bb]i[/color][COLOR=#007700]++ )
            {
                if ( ( [/COLOR][color=#0000bb]allowedChars [/color][color=#007700]+ [/color][color=#0000bb]negativeChars [/color][color=#007700]).[/color][color=#0000bb]indexOf[/color][color=#007700]( [/color][color=#0000bb]str[/color][color=#007700].[/color][color=#0000bb]charAt[/color][color=#007700]([/color][color=#0000bb]i[/color][color=#007700]) ) == -[/color][color=#0000bb]1[/color][COLOR=#007700])
                    return;
            }

            [/COLOR][color=#0000bb]int precisionLength [/color][color=#007700]= [/color][color=#0000bb]0[/color][color=#007700], [/color][color=#0000bb]dotLength [/color][color=#007700]= [/color][color=#0000bb]0[/color][color=#007700], [/color][color=#0000bb]minusLength [/color][color=#007700]= [/color][color=#0000bb]0[/color][COLOR=#007700];
            [/COLOR][color=#0000bb]int textLength [/color][color=#007700]= [/color][color=#0000bb]text[/color][color=#007700].[/color][color=#0000bb]length[/color][COLOR=#007700]();

            try
            {
                if ( [/COLOR][color=#0000bb]format [/color][color=#007700]== [/color][color=#0000bb]NUMERIC [/color][COLOR=#007700])
                {
                    if ( ! ( ( [/COLOR][color=#0000bb]text[/color][color=#007700].[/color][color=#0000bb]equals[/color][color=#007700]( [/color][color=#0000bb]negativeChars [/color][color=#007700]) ) && ( [/color][color=#0000bb]text[/color][color=#007700].[/color][color=#0000bb]length[/color][color=#007700]() == [/color][color=#0000bb]1[/color][COLOR=#007700]) ) )
                        new [/COLOR][color=#0000bb]Long[/color][color=#007700]([/color][color=#0000bb]text[/color][COLOR=#007700]);
                }
                else if ( [/COLOR][color=#0000bb]format [/color][color=#007700]== [/color][color=#0000bb]DECIMAL [/color][COLOR=#007700])
                {
                    if ( ! ( ( [/COLOR][color=#0000bb]text[/color][color=#007700].[/color][color=#0000bb]equals[/color][color=#007700]( [/color][color=#0000bb]negativeChars [/color][color=#007700]) ) && ( [/color][color=#0000bb]text[/color][color=#007700].[/color][color=#0000bb]length[/color][color=#007700]() == [/color][color=#0000bb]1[/color][COLOR=#007700]) ) )
                        new [/COLOR][color=#0000bb]Double[/color][color=#007700]([/color][color=#0000bb]text[/color][COLOR=#007700]);

                    [/COLOR][color=#0000bb]int dotIndex [/color][color=#007700]= [/color][color=#0000bb]text[/color][color=#007700].[/color][color=#0000bb]indexOf[/color][color=#007700]([/color][color=#0000bb]DOT[/color][COLOR=#007700]);
                    if( [/COLOR][color=#0000bb]dotIndex [/color][color=#007700]!= -[/color][color=#0000bb]1 [/color][COLOR=#007700])
                    {
                        [/COLOR][color=#0000bb]dotLength [/color][color=#007700]= [/color][color=#0000bb]1[/color][COLOR=#007700];
                        [/COLOR][color=#0000bb]precisionLength [/color][color=#007700]= [/color][color=#0000bb]textLength [/color][color=#007700]- [/color][color=#0000bb]dotIndex [/color][color=#007700]- [/color][color=#0000bb]dotLength[/color][COLOR=#007700];

                        if( [/COLOR][color=#0000bb]precisionLength [/color][color=#007700]> [/color][color=#0000bb]precision [/color][COLOR=#007700])
                            return;
                    }
                }
            }
            catch([/COLOR][color=#0000bb]Exception ex[/color][COLOR=#007700])
            {
                return;
            }

            if ( [/COLOR][color=#0000bb]text[/color][color=#007700].[/color][color=#0000bb]startsWith[/color][color=#007700]( [/color][color=#dd0000]"" [/color][color=#007700]+ [/color][color=#0000bb]NEGATIVE [/color][COLOR=#007700]) )
            {
                if ( ![/COLOR][color=#0000bb]allowNegative [/color][COLOR=#007700])
                    return;
                else
                    [/COLOR][color=#0000bb]minusLength [/color][color=#007700]= [/color][color=#0000bb]1[/color][COLOR=#007700];
            }

            if ( [/COLOR][color=#0000bb]maxLength [/color][color=#007700]< ( [/color][color=#0000bb]textLength [/color][color=#007700]- [/color][color=#0000bb]dotLength [/color][color=#007700]- [/color][color=#0000bb]precisionLength [/color][color=#007700]- [/color][color=#0000bb]minusLength [/color][COLOR=#007700]) )
                return;

            [/COLOR][color=#0000bb]super[/color][color=#007700].[/color][color=#0000bb]insertString[/color][color=#007700]( [/color][color=#0000bb]offset[/color][color=#007700], [/color][color=#0000bb]str[/color][color=#007700], [/color][color=#0000bb]attr [/color][COLOR=#007700]);
        }
    }
}  
[/COLOR][/COLOR]

}}}}
تم الشكر بواسطة:


التنقل السريع :


يقوم بقرائة الموضوع: بالاضافة الى ( 1 ) ضيف كريم