From 4f6f05beccafcadf7b6128c8e168440c3ab7dbdf Mon Sep 17 00:00:00 2001 From: "Hugh.C" Date: Thu, 12 Sep 2019 15:18:21 +0800 Subject: [PATCH] =?UTF-8?q?REPORT-21800=20=E6=B0=B4=E5=8D=B0=E5=AF=86?= =?UTF-8?q?=E5=BA=A6=E6=A1=86=E6=9C=80=E5=B0=8F=E9=99=90=E5=88=B6=E6=9C=89?= =?UTF-8?q?=E9=97=AE=E9=A2=98=EF=BC=8C=E5=88=A0=E6=8E=89=E8=BF=9B=E8=A1=8C?= =?UTF-8?q?=E8=BE=93=E5=85=A5=E7=9A=84=E8=AF=9D=EF=BC=8C=E5=B0=B1=E6=98=AF?= =?UTF-8?q?=E5=B0=8F=E4=BA=8E100=EF=BC=8C=E6=97=A0=E6=B3=95=E6=89=8B?= =?UTF-8?q?=E5=8A=A8=E8=BE=93=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gui/ispinner/UnsignedIntUISpinner.java | 43 ++++++++++++++++++- .../gui/itextfield/UIIntNumberField.java | 26 ++++++++--- 2 files changed, 61 insertions(+), 8 deletions(-) diff --git a/designer-base/src/main/java/com/fr/design/gui/ispinner/UnsignedIntUISpinner.java b/designer-base/src/main/java/com/fr/design/gui/ispinner/UnsignedIntUISpinner.java index 0a04eabf5..1515477a3 100644 --- a/designer-base/src/main/java/com/fr/design/gui/ispinner/UnsignedIntUISpinner.java +++ b/designer-base/src/main/java/com/fr/design/gui/ispinner/UnsignedIntUISpinner.java @@ -3,6 +3,9 @@ package com.fr.design.gui.ispinner; import com.fr.design.gui.itextfield.UIIntNumberField; import com.fr.design.gui.itextfield.UINumberField; +import java.awt.event.FocusEvent; +import java.awt.event.FocusListener; + /** * Created by IntelliJ IDEA. * Author : Hugh.C @@ -11,21 +14,59 @@ import com.fr.design.gui.itextfield.UINumberField; */ public class UnsignedIntUISpinner extends UISpinner { + private double minValue; + private double maxValue; public UnsignedIntUISpinner(double minValue, double maxValue, double dierta) { super(minValue, maxValue, dierta); + this.minValue = minValue; + this.maxValue = maxValue; } public UnsignedIntUISpinner(double minValue, double maxValue, double dierta, double defaultValue) { super(minValue, maxValue, dierta, defaultValue); + this.minValue = minValue; + this.maxValue = maxValue; } @Override protected UINumberField initNumberField() { - return new UIIntNumberField() { + final UIIntNumberField numberField = new UIIntNumberField() { public boolean shouldResponseChangeListener() { return false; } + + public NumberDocument createNumberDocument() { + return new NumberDocument() { + public boolean isContinueInsertWhenOverMaxOrMinValue() { + return true; + } + }; + } }; + numberField.addFocusListener(new FocusListener() { + @Override + public void focusGained(FocusEvent e) { + + } + + /** + * 失去焦点后再做范围限制、不然最小值为 100 时,输个 1 都不让.... + * @param e + */ + @Override + public void focusLost(FocusEvent e) { + double value = numberField.getValue(); + if (!isOverMaxOrMinValue(value)) { + return; + } + numberField.setValue(value < minValue ? minValue : maxValue); + } + + private boolean isOverMaxOrMinValue(double value) { + return value < minValue || value > maxValue; + } + }); + return numberField; } } \ No newline at end of file diff --git a/designer-base/src/main/java/com/fr/design/gui/itextfield/UIIntNumberField.java b/designer-base/src/main/java/com/fr/design/gui/itextfield/UIIntNumberField.java index 59e5e8825..37b1e74e4 100644 --- a/designer-base/src/main/java/com/fr/design/gui/itextfield/UIIntNumberField.java +++ b/designer-base/src/main/java/com/fr/design/gui/itextfield/UIIntNumberField.java @@ -1,11 +1,10 @@ package com.fr.design.gui.itextfield; -import com.fr.design.gui.itextfield.UINumberField; import javax.swing.text.AttributeSet; import javax.swing.text.BadLocationException; import javax.swing.text.PlainDocument; -import java.awt.*; +import java.awt.Toolkit; /** * Created with IntelliJ IDEA. @@ -15,11 +14,11 @@ import java.awt.*; * To change this template use File | Settings | File Templates. */ public class UIIntNumberField extends UINumberField { - public void setFieldDocument(){ - setDocument(new NumberDocument()); + public void setFieldDocument() { + setDocument(createNumberDocument()); } - class NumberDocument extends PlainDocument { + public class NumberDocument extends PlainDocument { public NumberDocument() { } @@ -33,17 +32,30 @@ public class UIIntNumberField extends UINumberField { String strNew = str.substring(0, offset) + s + str.substring(offset, getLength()); - if (isOverMaxOrMinValue(strNew)) { + if (isOverMaxOrMinValue(strNew) && !isContinueInsertWhenOverMaxOrMinValue()) { Toolkit.getDefaultToolkit().beep(); return; } - setisContentChanged(true); + setisContentChanged(true); super.insertString(offset, s, a); } + /** + * 是否继续插入输入的字符 - 当超出范围时 + * + * @return true : 继续插入输入的字符 + */ + public boolean isContinueInsertWhenOverMaxOrMinValue() { + return false; + } + private boolean isOverMaxOrMinValue( String strNew) { return (Double.parseDouble(strNew)getMaxValue()); } } + + public NumberDocument createNumberDocument() { + return new NumberDocument(); + } } \ No newline at end of file