如何将 JTextField 中的值解析为 Integer 并对其执行一些数学运算?

How can I parse a value from a JTextField into an Integer and perform some math operations on it?

所以,我一直在尝试使用 swing 在 Java 中制作一个摄氏转换器,但一直坚持从 JTextField 获取输入并将其解析为整数,这样我就可以执行上的等式。如果我将其保留为 String,我将无法进行任何数学运算。

我添加了一个名为 cValue 的私有字符串,我将文本字段的值存储在其中,然后我在 ActionListener 中有一些代码将该字符串解析为 Integer.

当我 运行 程序打开 window 时没有任何问题。我可以在文本字段中输入任何内容,但只要按下按钮,程序就会退出,并显示一个我无法理解的错误。如果我将代码从动作侦听器和 运行 程序中移出,它会给我一个错误。

现在,我对 Java 还很陌生,还不是很熟悉。我使用 eclipse 编写了这个,并使用 WindowBuilder 制作了 UI。我已经尝试了很多东西,但到目前为止没有任何效果。我很感激我能得到的任何形式的反馈。

这是代码:

private String cValue; 
private String result = "0";
private JPanel contentPane;
private JTextField celsiusField;
private JButton convertButton;

/**
 * Create the frame.
 */
public CelsiusConverter() {

    setDefaultCloseOperation(CelsiusConverter.EXIT_ON_CLOSE);
    setBounds(100, 100, 194, 134);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JLabel lblCelsius = new JLabel("Celsius");
    lblCelsius.setBounds(10, 11, 61, 14);
    contentPane.add(lblCelsius);

    celsiusField = new JTextField();
    celsiusField.setBounds(81, 8, 86, 20);
    contentPane.add(celsiusField);
    celsiusField.setColumns(10);

    JLabel lblFahrenheit = new JLabel("Fahrenheit:");
    lblFahrenheit.setBounds(10, 73, 70, 14);
    contentPane.add(lblFahrenheit);

    JLabel lblResult = new JLabel();
    lblResult.setText(String.valueOf(result));
    lblResult.setBounds(81, 73, 87, 14);
    contentPane.add(lblResult);     
    cValue = celsiusField.getText();

    convertButton = new JButton("Convert");
    convertButton.setBounds(10, 39, 157, 23);
    contentPane.add(convertButton);
    convertButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            int parsed = Integer.parseInt(cValue);
        }           
    });
}

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                CelsiusConverter frame = new CelsiusConverter();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

这是错误:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""

at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at learningWindowBuilder.CelsiusConverter.actionPerformed(CelsiusConverter.java:53)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access0(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
cValue = celsiusField.getText();

您还不能调用该语句,因为 GUI 甚至不可见,用户还没有机会在文本字段中输入数据。

您需要从 ActionListener 的文本字段中获取文本

String cValue = celsiusField.getText();
int parsed = Integer.parseInt(cValue);