これは、値の名前が正確ではありませんどのように機能するかを示すために、一例です。 このように考えてください。 JFormattedTextFieldには、整数を保持するlastValidIntというフィールドがあります。 フィールド内のテキストがフォーマットされるたびに、プログラムはフィールド内のテキストが数字かどうかをチェックします。そうであれば、lastValidIntはその文字列に設定されます。
lastValidIntが初期化されていて、それが持っているならば、それはlastValidIntの値を返す場合getValue()
方法を確認します。それがなければヌルを返します。
例: これらは、毎回アプリケーションを再起動していることを前提としています。 フィールドに入力する内容を入力します。 出力は、何かを入力した後にコンソールが印刷するものです。
1:
Input: a
Ouput: null
//a is not a number therefore the output is null
2:
Input: 1
Output: 1
//1 is a number therefore the output is 1
3:
Input1: a
Ouput: null
Input2: as
Ouput: null
Input3: asd
Ouput: null
Input4: asdf
Ouput: null
//a is not a number therefore the output is null
4:
Input1: 1
Ouput: 1
Input2: 1f
Ouput: 1
//The first thing the was read here was 1 therefore lastValidInt was set to 1 then the next input is 1f which is not a number therefore lastValidInt remained 1
5:
Input1: f
Ouput: null
Input2: f1
Ouput: null
//f is not a number therefore output is null, f1 is not a number either since it has a character that is not a digit and therefore lastValidInt has not been initialized
6:
Input1: f
Ouput: null
Input2:
Output: null
Input3: 1
Output: 1
Input4: 1f
Output: 1
//f is not a number therefore output is null, "" is also not a number therefore output is null again then the only thing that exists in the field is 1 which is a number therefore output is 1, finally by adding a f the field is no longer a number so output is same as before.
7:
Input1: f
Ouput: null
Input2: 1f (moved back to the begining without erasing f)
Output: null
//f is not a number therefore output is null, 1f is not a number either since it has a character that is not a digit and therefore lastValidInt has not been initialized
答えをくれてありがとうございますが、まず、私はこの試合をキャッチする必要があります。次に、このcommitEdit()関数が必要なのは分かりません。私はActionListenerを使用するとき、私はこの問題はありません。 – ardayigit
例外を処理するように更新されました。私は 'ActionListener'でコミットせずに動作する理由を理解していません。実装に違いがあると仮定することができます。 – StuPointerException
どうもありがとうございました。少なくともコードは動作しています:) – ardayigit