java - 为什么 JFormattedTextField 恢复以前的值?

标签 java swing jtextfield jformattedtextfield

我正在使用 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/

相关文章:

Java 对话框不释放

java - 使用 JTextField 调整文本

java - 如何在java中使用for循环清除文本字段

java - String.endsWith 的 StringBuffer 替代品是什么?

java - 我们可以在不强制类实现接口(interface)的情况下拥有多态性吗?

java - 将密码字符替换为 JPasswordField 中的点

java - 设置网格宽度时在 JDialog w/GridBagLayout 中重叠

java - JAXB XJC 抑制 ObjectFactory 并生成 jaxb.in​​dex

java - NoClassDefFoundError org/eclipse/swt/SWTError

java - 显示我的数据库中的名称时遇到问题