gpt4 book ai didi

java - 双重格式验证中的 JSpinner

转载 作者:行者123 更新时间:2023-11-29 05:12:45 24 4
gpt4 key购买 nike

我有一个 JFrame 女巫充当用户输入表单,它包含许多类型为 Double 格式的 JSpinner 字段。

例如:

 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)且用户单击“确定”按钮时,出现此错误。

她是完整的 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$2.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.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.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”,尽管两者有些相关并且很容易相互赋值。

关于java - 双重格式验证中的 JSpinner,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27783062/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com