如何阅读和使用java swing中文本字段中输入的数字

时间:2015-10-09 16:53:16

标签: java swing

这是一个愚蠢的问题,但我想读一个在Jtextfield中输入的数字。如何读取和使用在Java Swing中的文本字段中输入的整数或浮点数。谢谢。

我尝试了以下代码,但它无效

float tempFahr = (float)((Double.parseDouble(tempTextField.getText()))*1.8+32);
fahrenheitLabel.setText(tempFahr + " Fahrenheit");

给出以下异常

线程“AWT-EventQueue-0”中的异常java.lang.RuntimeException:无法编译的源代码 - 错误的树类型:     在SwingProgs.CelsiusConverterGUI.convertbuttonActionPerformed(CelsiusConverterGUI.java:89)     在SwingProgs.CelsiusConverterGUI.access $ 000(CelsiusConverterGUI.java:12)     在SwingProgs.CelsiusConverterGUI $ 1.actionPerformed(CelsiusConverterGUI.java:40)     在javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)     在javax.swing.AbstractButton $ Handler.actionPerformed(AbstractButton.java:2348)     在javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)     在javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)     在javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)     at java.awt.Component.processMouseEvent(Component.java:6535)     在javax.swing.JComponent.processMouseEvent(JComponent.java:3324)     at java.awt.Component.processEvent(Component.java:6300)     at java.awt.Container.processEvent(Container.java:2236)     at java.awt.Component.dispatchEventImpl(Component.java:4891)     at java.awt.Container.dispatchEventImpl(Container.java:2294)     at java.awt.Component.dispatchEvent(Component.java:4713)     at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)     at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)     at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)     at java.awt.Container.dispatchEventImpl(Container.java:2280)     at java.awt.Window.dispatchEventImpl(Window.java:2750)     at java.awt.Component.dispatchEvent(Component.java:4713)     at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)     at java.awt.EventQueue.access $ 500(EventQueue.java:97)     at java.awt.EventQueue $ 3.run(EventQueue.java:709)     at java.awt.EventQueue $ 3.run(EventQueue.java:703)     at java.security.AccessController.doPrivileged(Native Method)     at java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)     at java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)     at java.awt.EventQueue $ 4.run(EventQueue.java:731)     at java.awt.EventQueue $ 4.run(EventQueue.java:729)     at java.security.AccessController.doPrivileged(Native Method)     at java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)     at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)     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)     在java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

1 个答案:

答案 0 :(得分:-1)

代码片段似乎没有任何问题。运行时异常指向编译问题。

你看过这个链接吗? java.lang.RuntimeException: Uncompilable source code - what can cause this?