「労働者が自分の仕事をうまくやりたいなら、まず自分の道具を研ぎ澄まさなければなりません。」 - 孔子、「論語。陸霊公」
表紙 > プログラミング > jtextfieldは整数入力のみをどのように制限しますか?

jtextfieldは整数入力のみをどのように制限しますか?

2025-04-16に投稿されました
ブラウズ:144

How to Effectively Restrict JTextField Input to Only Integers?

jtextfield入力を整数に閉じ込める

jtextfieldコントロールのポジティブ整数にユーザー入力を制限することは、プログラミングにおいて一般的な問題です。この目的のためにキーリストナーを利用しようとしましたが、より効果的なアプローチがあります。

keylistenerに依存するのではなく、ドキュメントフィルターを実装することは、いくつかの利点を提供します。指定された基準。

    それは、データ型(例えば、入力されたデータを整数として解析できることを保証する)やデータの長さ(例えば、最大数桁数を施行する)などの数値範囲チェックを超えて追加の検証を可能にします。例:
  • Import javax.swing.text.plaindocument; javax.swing.text.documentfilterをインポートします。 javax.swing.text.attributesetをインポートします。 javax.swing.text.badlocationexceptionをインポートします。 class intdocumentfilterはslaindocumentを拡張します{ @オーバーライド public void insertString(filterbypass fb、int offset、string string、astributeset attr)throws badlocationexception { if(string == null || string.isempty()){ super.insertstring(fb、offset、string、attr); } それ以外 { 試す { integer.parseint(string); super.insertstring(fb、offset、string、attr); } catch(numberformatexception e){ //ユーザーに警告し、挿入を許可しないでください } } } @オーバーライド public voidの交換(filterbypass fb、int offset、int length、string text、astributeset attrs)badlocationexception { if(text == null || text.isempty()){ super.replace(fb、offset、length、text、attrs); } それ以外 { 試す { integer.parseint(テキスト); super.replace(fb、offset、length、text、attrs); } catch(numberformatexception e){ //ユーザーに警告し、挿入を許可しないでください } } } }
  • このフィルターを使用するには、インスタンス化してJTextfieldに関連付けられたPlainDocumentオブジェクトに設定します:

PlainDocument doc =(plainDocument)textfield.getDocument(); doc.setDocumentFilter(new intdocumentfilter());

import javax.swing.text.PlainDocument;
import javax.swing.text.DocumentFilter;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;

class IntDocumentFilter extends PlainDocument {

    @Override
    public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {

        if (string == null || string.isEmpty()) {
            super.insertString(fb, offset, string, attr);
        } else {
            try {
                Integer.parseInt(string);
                super.insertString(fb, offset, string, attr);
            } catch (NumberFormatException e) {
                // warn the user and don't allow the insert
            }
        }
    }

    @Override
    public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {

        if (text == null || text.isEmpty()) {
            super.replace(fb, offset, length, text, attrs);
        } else {
            try {
                Integer.parseInt(text);
                super.replace(fb, offset, length, text, attrs);
            } catch (NumberFormatException e) {
                // warn the user and don't allow the insert
            }
        }
    }
}

入力文字列を整数として解析できるかどうかを確認します。ユーザーは、nullまたは空の文字列を挿入しようとします。
最新のチュートリアル もっと>

免責事項: 提供されるすべてのリソースの一部はインターネットからのものです。お客様の著作権またはその他の権利および利益の侵害がある場合は、詳細な理由を説明し、著作権または権利および利益の証拠を提出して、電子メール [email protected] に送信してください。 できるだけ早く対応させていただきます。

Copyright© 2022 湘ICP备2022001581号-3