双重格式验证中的 JSpinner
JSpinner in Double format validation
我有一个 JFrame 女巫充当用户输入表单,它包含许多 JSpinner 类型的字段,格式为 Double。
例如:
Double current = 0.0;
Double min = (double) Integer.MIN_VALUE;
Double max = (double) Integer.MAX_VALUE;
Double step = 0.1;
JSpinner mySpinnerExample = new JSpinner(new SpinnerNumberModel(current, min, max, step));
当 mySpinnerExample 为空(默认值为 0)且用户单击 O.K 按钮时,出现此错误。
她是完整的 StackTrace
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.Double
at VIEW.ProduitUi.getFormData(ProduitUi.java:530)
at CONTROLLER.ProduitController.getUiFormData(ProduitController.java:124)
at CONTROLLER.ProduitController.createProduit(ProduitController.java:106)
at VIEW.ProduitUi.actionPerformed(ProduitUi.java:284)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6527)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6292)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4883)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4705)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4705)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access0(EventQueue.java:97)
at java.awt.EventQueue.run(EventQueue.java:697)
at java.awt.EventQueue.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue.run(EventQueue.java:719)
at java.awt.EventQueue.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
我该如何克服这个问题。
您的堆栈跟踪表明您的软件需要 Double,但得到的是 BigDecimal。
尝试在构建 SpinnerNumberModel 时将 Double 替换为 double。我怀疑 java 使用了这个构造函数
SpinnerNumberModel(Number value, Comparable minimum, Comparable maximum, Number stepSize)
而不是这个
SpinnerNumberModel(double value, double minimum, double maximum, double stepSize)
(见javadoc)
所以你的代码应该是
double current = 0.0;
double min = (double) Integer.MIN_VALUE;
double max = (double) Integer.MAX_VALUE;
double step = 0.1;
JSpinner mySpinnerExample = new JSpinner(new SpinnerNumberModel(current, min, max, step))
"Double" 也是 "Number"、"BigDecimal",但 "Double" 不是 "double",尽管两者有些相关并且很容易分配给彼此。
我有一个 JFrame 女巫充当用户输入表单,它包含许多 JSpinner 类型的字段,格式为 Double。
例如:
Double current = 0.0;
Double min = (double) Integer.MIN_VALUE;
Double max = (double) Integer.MAX_VALUE;
Double step = 0.1;
JSpinner mySpinnerExample = new JSpinner(new SpinnerNumberModel(current, min, max, step));
当 mySpinnerExample 为空(默认值为 0)且用户单击 O.K 按钮时,出现此错误。
她是完整的 StackTrace
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.Double
at VIEW.ProduitUi.getFormData(ProduitUi.java:530)
at CONTROLLER.ProduitController.getUiFormData(ProduitController.java:124)
at CONTROLLER.ProduitController.createProduit(ProduitController.java:106)
at VIEW.ProduitUi.actionPerformed(ProduitUi.java:284)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6527)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6292)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4883)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4705)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4705)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access0(EventQueue.java:97)
at java.awt.EventQueue.run(EventQueue.java:697)
at java.awt.EventQueue.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue.run(EventQueue.java:719)
at java.awt.EventQueue.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
我该如何克服这个问题。
您的堆栈跟踪表明您的软件需要 Double,但得到的是 BigDecimal。
尝试在构建 SpinnerNumberModel 时将 Double 替换为 double。我怀疑 java 使用了这个构造函数
SpinnerNumberModel(Number value, Comparable minimum, Comparable maximum, Number stepSize)
而不是这个
SpinnerNumberModel(double value, double minimum, double maximum, double stepSize)
(见javadoc)
所以你的代码应该是
double current = 0.0;
double min = (double) Integer.MIN_VALUE;
double max = (double) Integer.MAX_VALUE;
double step = 0.1;
JSpinner mySpinnerExample = new JSpinner(new SpinnerNumberModel(current, min, max, step))
"Double" 也是 "Number"、"BigDecimal",但 "Double" 不是 "double",尽管两者有些相关并且很容易分配给彼此。