有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Swing数字格式异常

出于某种原因,下面的AddListener类无法工作,我不断收到数字格式异常。谁能告诉我原因吗。在我看来,这似乎应该行得通

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class BabyCalculator extends JFrame {

    JFrame theFrame = this;
    JTextField addField; // Declaring this here so that you can access the variable from other places. MAKE SURE TO NOT DECLARE THIS AGAIN IN THE CONSTRUCTOR
    JTextField totalField;

    public BabyCalculator() {
        //You set this up so that you can refer to the frame using the inner class below.
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setName("Baby Calculator");
        setLayout(new GridLayout(3, 0));
        //add
        JLabel addLabel = new JLabel("Amount to add:");
        addField = new JTextField(10);
        JButton addButton = new JButton("add");
        addButton.addActionListener(new AddListener());
        //multiply
        JLabel multiplyLabel = new JLabel("Amount to multiply:");
        JTextField multiplyField = new JTextField(10);
        JButton multiplyButton = new JButton("multiply");
        //total
        JLabel totalLabel = new JLabel("Total");
        totalField = new JTextField(10);
        totalField.setEditable(false);
        JButton stopButton = new JButton("Stop");
        stopButton.addActionListener(new StopListener());
        //Create Panels
        JPanel topRow = new JPanel(new BorderLayout());
        JPanel middleRow = new JPanel(new BorderLayout());
        JPanel bottomRow = new JPanel(new FlowLayout());
        //Add the top Row
        topRow.add(addLabel, BorderLayout.WEST);
        topRow.add(addField, BorderLayout.CENTER);
        topRow.add(addButton, BorderLayout.EAST);
        add(topRow);

        middleRow.add(multiplyLabel, BorderLayout.WEST);
        middleRow.add(multiplyField, BorderLayout.CENTER);
        middleRow.add(multiplyButton, BorderLayout.EAST);
        add(middleRow);

        bottomRow.add(totalLabel);
        bottomRow.add(totalField);
        bottomRow.add(stopButton);

        add(bottomRow);


        pack();
        setVisible(true);
    }


    public class AddListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            String addFieldText = addField.getText();
            String totalFieldText = totalField.getText();
            double addAmount = Double.parseDouble(addFieldText);
            double total = Double.parseDouble(totalFieldText);
            total += addAmount;
            totalField.setText(total + "");

        }
    }
    //end class AddListener

    public class StopListener implements ActionListener {//this is an inner class

        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(theFrame, "You Clicked the stop button");
        }//end class StopListener
    }

    public static void main(String[] args) {
        JFrame newFrame = new BabyCalculator();
    }
}

注意:代码中的问题肯定与AddListener有关。每当我在GUI中单击add按钮时,就会出现异常


共 (1) 个答案

  1. # 1 楼答案

    问题在于totalFieldText,它的默认值为空,这意味着当您尝试转换为double值时,它会导致NumberFormatException

    例如,尝试给它一个默认值0

    totalField = new JTextField("0", 10);
    

    你可能还想看看How to Use SpinnersHow to Use Formatted Text Fields,这会让你的生活更轻松