diff --git a/designer-base/src/main/java/com/fr/design/gui/itextfield/UINumberField.java b/designer-base/src/main/java/com/fr/design/gui/itextfield/UINumberField.java index f4437a9c4a..62d24c56a4 100644 --- a/designer-base/src/main/java/com/fr/design/gui/itextfield/UINumberField.java +++ b/designer-base/src/main/java/com/fr/design/gui/itextfield/UINumberField.java @@ -5,11 +5,15 @@ import com.fr.general.ComparatorUtils; import com.fr.stable.CoreConstants; import com.fr.stable.StringUtils; +import javax.swing.event.CaretEvent; +import javax.swing.event.CaretListener; import javax.swing.text.AttributeSet; import javax.swing.text.BadLocationException; import javax.swing.text.PlainDocument; import java.awt.Dimension; import java.awt.Toolkit; +import java.awt.event.InputMethodEvent; +import java.awt.event.InputMethodListener; /** * Number Field. @@ -35,6 +39,11 @@ public class UINumberField extends UITextField { private boolean isContentChanged = false; private boolean fillNegativeNumber = true; + /** + * 输入法输入完成一个字符输入过程的标记。开始输入的时候是true,输入完矫正光标后为false + */ + private boolean isInputMethodOnceInserting = false; + private int insertOffset = 0; public UINumberField() { this(MAX_INTEGERLENGTH_32, MAX_DECIMALLENGTH); @@ -55,6 +64,7 @@ public class UINumberField extends UITextField { this.minValue = minValue; this.maxValue = maxValue; setFieldDocument(); + initFieldListener(); } public void setFieldDocument() { @@ -62,6 +72,31 @@ public class UINumberField extends UITextField { initListener(); } + private void initFieldListener() { + addInputMethodListener(new InputMethodListener() { + @Override + public void inputMethodTextChanged(InputMethodEvent event) { + isInputMethodOnceInserting = true; + } + + @Override + public void caretPositionChanged(InputMethodEvent event) { + } + }); + + addCaretListener(new CaretListener() { + @Override + public void caretUpdate(CaretEvent e) { + if (isInputMethodOnceInserting) { + isInputMethodOnceInserting = false; + setCaretPosition(insertOffset); + } else { + insertOffset = e.getDot(); + } + } + }); + } + public void canFillNegativeNumber(boolean fillNegativeNumber) { this.fillNegativeNumber = fillNegativeNumber; } @@ -186,6 +221,13 @@ public class UINumberField extends UITextField { super.insertString(offset, s, a); } + public void remove(int offs, int len) throws BadLocationException { + if (isInputMethodOnceInserting) { + return; + } + super.remove(offs, len); + } + // kunsnat: 这种限制输入 有个不好的地方, 比如删除时: 10.1 最大值限定100, 那么就删除中间的小数点之后变为101, 超出了100. // 但是直接限制不能删除中间类似小数点, 那么也可能遇到: 最小值10 , 从100变化到其中的19, 就很难.. private boolean notChange(String strNew) {