简体   繁体   English

double类型的方法parsedouble字符串不适用于double参数

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

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 double类型的方法parsedouble字符串不适用于double参数

I get the error with number1, number2 and result, when I try to convert the text to double and the opposite. 当我尝试将文本转换为double和相反时,出现数字1,数字2和结果错误。

I can't convert my JTextField to a String this will affect the frame I've created 我无法将JTextField转换为String,这会影响我创建的框架

Here is my Code: import java.awt.*; 这是我的代码: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();
}
}

Use 采用

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

to parse the actual contents of the JTextField . 解析JTextField的实际内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PrintStream 类型中的方法 println(double) 不适用于 arguments (String, double) - The method println(double) in the type PrintStream is not applicable for the arguments (String, double) Method(double, double) 不适用于参数 (double[], double[]) - Method(double, double) is not applicable for the arguments (double[], double[]) 如何解决 main 类型中的方法 add(double, double, int, double) 不适用于参数 (double, double, double, double) - how to solve The method add(double, double, int, double) in the type main is not applicable for the arguments (double, double, double, double) main类型的方法(double [])不适用于参数(int []) - The method (double[]) in the type main is not applicable for the arguments (int[]) PathIterator类型的currentSegment(float [])方法不适用于参数(Double []) - The method currentSegment(float[]) in the type PathIterator is not applicable for the arguments (Double[]) Java Writer 错误:Writer 类型中的方法 write(char[], int, int) 不适用于 arguments (int, double, int, String) - Java Writer error: The method write(char[], int, int) in the type Writer is not applicable for the arguments (int, double, int, String) WebElement类型的方法sendKeys(CharSequence…)不适用于参数(双精度) - The method sendKeys(CharSequence…) in the type WebElement is not applicable for the arguments (double) Java:printf不适用于参数(字符串,双精度) - Java: printf is not applicable for the arguments (String, double) 如何将文本文件存储到数组 <double> 使用Double.parseDouble(string s)方法 - How to store a text file to an Array <double> using the Double.parseDouble(string s) method NumberFormatException: 空字符串; Double.parseDouble - NumberFormatException: empty String ; Double.parseDouble
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM