从 jformatted 文本字段中获取浮点值

发布于 2024-09-24 06:52:30 字数 473 浏览 0 评论 0原文

我能够创建仅接受浮点值的 JFormatted TextField,但我无法获取该值...... 我声明它..

stopAppFormattedTextField = new javax.swing.JFormattedTextField(new DecimalFormat("#.00"));

并使用 : 获取值

double stop=(Double)stopAppFormattedTextField.getValue();

,但上面的语句抛出以下异常:

“线程异常 “AWT-EventQueue-0” java.lang.ClassCastException: java.lang.Long 无法转换为 java.lang.Double"

我应该怎么做才能获取浮点值? -提前致谢

I am able to create JFormatted TextField that accepts only float values,but I am not able to fetch that value....
I am declaring it ..

stopAppFormattedTextField = new javax.swing.JFormattedTextField(new DecimalFormat("#.00"));

and fetching the value using :

double stop=(Double)stopAppFormattedTextField.getValue();

but the above statement is throwing the following exception:

"Exception in thread
"AWT-EventQueue-0"
java.lang.ClassCastException:
java.lang.Long cannot be cast to
java.lang.Double"

what should I do to fetch float values?
-Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绻影浮沉 2024-10-01 06:52:30

您从格式化程序中获取了Long,但您需要double。你可以这样做:

Number number = (Number)stopAppFormattedTextField.getValue();
double stop = number.doubleValue();

You're getting Longs from the formatter, but you want doubles. You can do this:

Number number = (Number)stopAppFormattedTextField.getValue();
double stop = number.doubleValue();
如果没有 2024-10-01 06:52:30

初始化后将其添加到您的代码中:

stopAppFormattedTextField.setValue(0d);

getValue 将自动神奇地返回 double

Add this to your code after initialization:

stopAppFormattedTextField.setValue(0d);

The getValue will return double auto-magically

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文