the method parsedouble string in the type double is not applicable for the arguments double

时间:2016-02-12 19:25:16

标签: java

I'm trying to create a simple calculator but I get this error when I try to add the action performed to the button.

the method parsedouble string in the type double is not applicable for the arguments double

I get the error with number1, number2 and result, when I try to convert the text to double and the opposite.

I can't convert my JTextField to a String this will affect the frame I've created

Here is my Code: import java.awt.*;

public class ShowGridLayout extends JFrame { // Declaring the class

public ShowGridLayout() {
    getContentPane().setLayout(new GridLayout(4,2));
    JLabel label = new JLabel("First Number");
    getContentPane().add(label);
    JTextField text = new JTextField(8);
    getContentPane().add(text);
    JLabel label1 = new JLabel("Second Number");
    getContentPane().add(label1);
    JTextField text1 = new JTextField();
    getContentPane().add(text1);
    JLabel label2 = new JLabel("Result is");
    getContentPane().add(label2);
    JTextField text2 = new JTextField();
    text2.setEditable(false);
    getContentPane().add(text2);
    JButton btn = new JButton("Click here");
    getContentPane().add(btn);
    setVisible(true);
    setSize(400,200);
    setResizable(false);
    setLocationRelativeTo(null);

    btn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            double number1, number2, result;
            try {
                number1 = Double.parseDouble(text);
                number2 = Double.parseDouble(text1);
                result = number1*number2;
                text2.setText(Double.toString(result));
            } catch(Exception e1){
                JOptionPane.showMessageDialog(null, "Please add a number");
            }
        }
    });
}



public static void main (String args[]) { // Creating the main method
    ShowGridLayout frame = new ShowGridLayout();
}
}

1 个答案:

答案 0 :(得分:1)

Use

number1 = Double.parseDouble(text.getText());
number2 = Double.parseDouble(text1.getText());

to parse the actual contents of the JTextField.

相关问题