Browse Source

CHART-18080 修复数字输入框输入中文时自动删除数字问题

zheng-1641779399395
kuangshuai 4 years ago
parent
commit
434f4a0f56
  1. 39
      designer-base/src/main/java/com/fr/design/gui/itextfield/UINumberField.java

39
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,8 @@ public class UINumberField extends UITextField {
private boolean isContentChanged = false;
private boolean fillNegativeNumber = true;
private boolean isInputMethodChange = false;
private int insertOffset = 0;
public UINumberField() {
this(MAX_INTEGERLENGTH_32, MAX_DECIMALLENGTH);
@ -55,6 +61,7 @@ public class UINumberField extends UITextField {
this.minValue = minValue;
this.maxValue = maxValue;
setFieldDocument();
initFieldListener();
}
public void setFieldDocument() {
@ -62,6 +69,31 @@ public class UINumberField extends UITextField {
initListener();
}
private void initFieldListener() {
addInputMethodListener(new InputMethodListener() {
@Override
public void inputMethodTextChanged(InputMethodEvent event) {
isInputMethodChange = true;
}
@Override
public void caretPositionChanged(InputMethodEvent event) {
}
});
addCaretListener(new CaretListener() {
@Override
public void caretUpdate(CaretEvent e) {
if (isInputMethodChange) {
isInputMethodChange = false;
setCaretPosition(insertOffset);
} else {
insertOffset = e.getDot();
}
}
});
}
public void canFillNegativeNumber(boolean fillNegativeNumber) {
this.fillNegativeNumber = fillNegativeNumber;
}
@ -186,6 +218,13 @@ public class UINumberField extends UITextField {
super.insertString(offset, s, a);
}
public void remove(int offs, int len) throws BadLocationException {
if (isInputMethodChange) {
return;
}
super.remove(offs, len);
}
// kunsnat: 这种限制输入 有个不好的地方, 比如删除时: 10.1 最大值限定100, 那么就删除中间的小数点之后变为101, 超出了100.
// 但是直接限制不能删除中间类似小数点, 那么也可能遇到: 最小值10 , 从100变化到其中的19, 就很难..
private boolean notChange(String strNew) {

Loading…
Cancel
Save