"柴比爪哇";AWT-EventQueue-0“;java.lang.NumberFormatException:多个点

"柴比爪哇";AWT-EventQueue-0“;java.lang.NumberFormatException:多个点,java,swing,Java,Swing,我是第一次练习JAVA的学生。我们被要求创建一个简单的计算器。我做的每件事都没有错。我唯一的问题是,当我做一个1.66+55,甚至5+6的例子时,它显示了标题中的错误。这是女同事 import java.awt.BorderLayout; 公共类Frame2扩展了JFrame{ private JPanel contentPane; private JTextField tot; public String operateur = ""; public String s; public St

我是第一次练习JAVA的学生。我们被要求创建一个简单的计算器。我做的每件事都没有错。我唯一的问题是,当我做一个1.66+55,甚至5+6的例子时,它显示了标题中的错误。这是女同事

import java.awt.BorderLayout;
公共类Frame2扩展了JFrame{

private JPanel contentPane;
private JTextField tot;
public String operateur = ""; 
public String s;
public String Value = "";
public String Value2 = "";
public Double Total = 0.0;


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

/**
 * Create the frame.
 */
public Frame2() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 267, 360);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    tot = new JTextField();
    tot.setFont(new Font("Tahoma", Font.PLAIN, 18));
    tot.setEditable(false);
    tot.setBounds(10, 11, 231, 45);
    contentPane.add(tot);
    tot.setColumns(10);

    JButton t7 = new JButton("7");
    t7.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (operateur == "") { 
            Value = Value.trim() + 7;
            } else {
            Value2 = Value2.trim() + 7;
            }
            s = tot.getText();
            tot.setText(s+"7");
        }
    });
    t7.setBounds(10, 67, 49, 41);
    contentPane.add(t7);

    JButton t8 = new JButton("8");
    t8.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            if (operateur == "") { 
            Value = Value.trim() + 8;
            } else {
            Value2 = Value2.trim() + 8;
            }
            s = tot.getText();
            tot.setText(s+"8");


        }
    });
    t8.setBounds(69, 67, 50, 41);
    contentPane.add(t8);

    JButton t9 = new JButton("9");
    t9.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (operateur == "") { 
            Value = Value.trim() + 9;
            } else {
            Value2 = Value2.trim() + 9;
            }
            s = tot.getText();
            tot.setText(s+"9");
        }
    });
    t9.setBounds(129, 67, 50, 41);
    contentPane.add(t9);

    JButton t4 = new JButton("4");
    t4.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (operateur == "") { 
            Value = Value.trim() + 4;
            } else {
            Value2 = Value2.trim() + 4;
            }
            s = tot.getText();
            tot.setText(s+"4");
        }
    });
    t4.setBounds(10, 119, 50, 41);
    contentPane.add(t4);

    JButton t5 = new JButton("5");
    t5.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (operateur == "") { 
            Value = Value.trim() + 5;
            } else {
            Value2 = Value2.trim() + 5;
            }
            s = tot.getText();
            tot.setText(s+"5");
        }
    });
    t5.setBounds(69, 119, 50, 41);
    contentPane.add(t5);

    JButton t6 = new JButton("6");
    t6.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (operateur == "") { 
            Value = Value.trim() + 6.0;
            } else {
            Value2 = Value2.trim() + 6.0;
            }
            s = tot.getText();
            tot.setText(s+"6");
        }
    });
    t6.setBounds(129, 119, 50, 41);
    contentPane.add(t6);

    JButton t3 = new JButton("3");
    t3.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (operateur == "") { 
            Value = Value.trim() + 3.0;
            } else {
            Value2 = Value2.trim() + 3.0;
            }
            s = tot.getText();
            tot.setText(s+"3");
        }
    });
    t3.setBounds(10, 173, 49, 39);
    contentPane.add(t3);

    JButton t2 = new JButton("2");
    t2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (operateur == "") { 
            Value = Value.trim() + 2;
            } else {
            Value2 = Value2.trim() + 2;
            }
            s = tot.getText();
            tot.setText(s+"2");
        }
    });
    t2.setBounds(69, 171, 50, 41);
    contentPane.add(t2);

    JButton t1 = new JButton("1");

    t1.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            if (operateur == "") { 
            Value = Value + 1;
            } else if (operateur != null) {
            Value2 = Value2.trim() + 1;
            }
            s = tot.getText();
            tot.setText(s+"1");


        }
    });
    t1.setBounds(129, 171, 49, 41);
    contentPane.add(t1);

    JButton t0 = new JButton("0");
    t0.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (operateur == "") { 
            Value = Value.trim() + 0;
            } else {
            Value2 = Value2.trim() + 0;
            }
            s = tot.getText();
            tot.setText(s+"0");
        }
    });
    t0.setBounds(129, 221, 50, 41);
    contentPane.add(t0);

    JButton virg = new JButton(",");
    virg.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (operateur == "") { 
                Value = Value.trim() + ".";
                } else {
                Value2 = Value2.trim() + ".";
                }
                s = tot.getText();
                tot.setText(s+".");

        }
    });
    virg.setBounds(69, 223, 50, 39);
    contentPane.add(virg);

    JButton btnC = new JButton("C");
    btnC.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            tot.setText("");
            operateur = "";
            Value = "";
            Value2 = "";
            Total = 0.0;

        }
    });
    btnC.setForeground(Color.RED);
    btnC.setBounds(10, 223, 49, 41);
    contentPane.add(btnC);

    JButton plus = new JButton("+");
    plus.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            operateur ="+";
            s = tot.getText();
            tot.setText(s +"+");



        }
    });
    plus.setBounds(188, 67, 53, 41);
    contentPane.add(plus);

    JButton calc = new JButton("CALCULER");
    calc.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            if (operateur == "+") {
            Total = Double.valueOf(Value) + Double.valueOf(Value2);
            Total = Math.round( Total * 100.0 ) / 100.0;
            tot.setText(String.valueOf(Total));

        }
        else if (operateur=="-") {
            Total = Double.valueOf(Value) - Double.valueOf(Value2);
            Total = Math.round( Total * 100.0 ) / 100.0;
            tot.setText(String.valueOf(Total));
        }
        else if (operateur=="*") {
            Total = Double.valueOf(Value) * Double.valueOf(Value2);
            Total = Math.round( Total * 100.0 ) / 100.0;
            tot.setText(String.valueOf(Total));
        }
        else if (operateur=="/") {
            Total = Double.valueOf(Value) / Double.valueOf(Value2);
            Total = Math.round( Total * 100.0 ) / 100.0;
            tot.setText(String.valueOf(Total));
        }
            Value = String.valueOf(Total);
            Value2 = "";
            operateur = "";
            Total = 0.0;
        }
    });
    calc.setBounds(10, 273, 231, 38);
    contentPane.add(calc);

    JButton moins = new JButton("-");
    moins.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            operateur ="-";
            s = tot.getText();
            tot.setText(s+"-");


        }
    });
    moins.setBounds(189, 119, 52, 41);
    contentPane.add(moins);

    JButton fois = new JButton("*");
    fois.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            operateur ="*";
            s = tot.getText();
            tot.setText(s+"*");



        }
    });
    fois.setBounds(188, 173, 53, 39);
    contentPane.add(fois);

    JButton sur = new JButton("/");
    sur.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            operateur ="/";
            s = tot.getText();
            tot.setText(s+"/");
        }
    });
    sur.setBounds(189, 221, 52, 41);
    contentPane.add(sur);
}

}

所以,你有很多问题可能会回来咬你,但让我们看看眼前的问题

public void actionPerformed(ActionEvent e) {
    if (operateur == "") { 
        Value = Value.trim() + 6.0;
    } else {
        Value2 = Value2.trim() + 6.0;
    }
    s = tot.getText();
    tot.setText(s+"6");
}
每次按6键时,它都会在
字符串的末尾追加
6.0
,因此当您键入
1.66
时,您实际上得到的是
1.6.06.0
…这显然不是您想要的

+6.0
更改为
+6
…并对
+3.0
执行相同操作


另外,看看和,它将使人们更容易阅读您的代码,也使您更容易阅读其他人的代码。因此,您有许多问题,这些问题可能会再次出现并困扰您,但让我们看看眼前的问题

public void actionPerformed(ActionEvent e) {
    if (operateur == "") { 
        Value = Value.trim() + 6.0;
    } else {
        Value2 = Value2.trim() + 6.0;
    }
    s = tot.getText();
    tot.setText(s+"6");
}
每次按6键时,它都会在
字符串的末尾追加
6.0
,因此当您键入
1.66
时,您实际上得到的是
1.6.06.0
…这显然不是您想要的

+6.0
更改为
+6
…并对
+3.0
执行相同操作


另外,还可以查看和,它将使人们更容易阅读您的代码,也使您更容易阅读他人

避免使用
null
布局,像素完美的布局在现代ui设计中是一种幻觉。影响零部件单个尺寸的因素太多,您无法控制。Swing的设计初衷是与布局管理器一起工作,丢弃这些布局管理器将导致无止境的问题,您将花费越来越多的时间来纠正这些问题。有关更多详细信息,请参阅Java中的
String
比较是通过
String.equals
方法完成的,而不是
=
operateur==“应该是
”。equals(operateur)
。您需要将一些调试信息放入代码中,并/或使用调试器检查发生了什么。当我看它时,
Value
等于
1.6.06.0
当尝试计算时…避免使用
null
布局,像素完美的布局在现代ui设计中是一种错觉。影响零部件单个尺寸的因素太多,您无法控制。Swing的设计初衷是与布局管理器一起工作,丢弃这些布局管理器将导致无止境的问题,您将花费越来越多的时间来纠正这些问题。有关更多详细信息,请参阅Java中的
String
比较是通过
String.equals
方法完成的,而不是
=
operateur==“应该是
”。equals(operateur)
。您需要将一些调试信息放入代码中,并/或使用调试器检查发生了什么。当我查看它时,
等于尝试计算时的
1.6.06.0
。。。