我正在使用 JFormattedTextField,当我尝试删除字段中的值时,它会自动恢复以前的值。 这种行为的原因是什么?我怎样才能阻止它恢复值(value)?
这是 JFormattedtextField 的代码。
jFormattedTextField2 = new javax.swing.JFormattedTextField(new DecimalFormat("###,###.##"));
最佳答案
发生这种情况是因为 JFormattedTextField
允许使用 setFocusLostBehavior
方法配置失去焦点时应采取的操作。
这些是操作。
Description
JFormattedTextField.REVERT
- Revert the display to match that of getValue, possibly losing the current edit.
JFormattedTextField.COMMIT
- Commits the current value. If the value being edited isn't considered a legal value by the AbstractFormatter that is, a ParseException is thrown, then the value will not change, and then edited value will persist.
JFormattedTextField.COMMIT_OR_REVERT
- Similar to COMMIT, but if the value isn't legal, behave like REVERT.
JFormattedTextField.PERSIST
- Do nothing, don't obtain a new AbstractFormatter, and don't update the value.
**** 默认值为 JFormattedTextField.COMMIT_OR_REVERT
因此,当您输入无效值时,它会被还原并获得之前的值
一致状态**
关于java - 为什么 JFormattedTextField 恢复以前的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17118600/