带 GUI 的二次公式只是 returns NaN

Quadratic formula with GUI just returns NaN

我正在尝试编写一个可以执行二次公式的程序。我也把它作为一个控制台程序来完成并且它有效但现在我想用 GUI 来完成它。现在我的问题是,它只显示 NaN。我已经从工作控制台程序中复制了公式,所以这不是问题所在。我认为它必须与解析有关。这里可以看到代码:

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

public class Mitternachtsformel implements ActionListener {
JFrame frame;
JPanel panel;
JPanel panel1;
JPanel panel2;
JPanel panel3;
JTextField text;
JTextField text1;
JTextField text2;
JLabel label;
JLabel label1;
JLabel label2;
JTextArea textA;
double aZahl;
double bZahl;
double cZahl;
double x1;
double x2;


public static void main(String[] args) {
    Mitternachtsformel MF = new Mitternachtsformel();
    MF.launchScreen();

}
public void launchScreen() {
    //frame erstellen
    frame = new JFrame("Mitternachtsformel");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500, 500);
    //font erstellen
    Font font = new Font("impact", 50, 50);
    //panel erstellen
    panel = new JPanel();
    panel.setSize(500, 500);
    panel.setBackground(Color.white);
    //panel1 erstellen
    panel1 = new JPanel();
    panel1.setSize(500, 500);
    panel1.setBackground(Color.white);
    panel1.setVisible(false);
    //panel1 erstellen
    panel2 = new JPanel();
    panel2.setSize(500,  500);
    panel2.setBackground(Color.white);
    panel2.setVisible(false);
    //panel3 erstellen
    panel3 = new JPanel();
    panel3.setSize(500, 500);
    panel3.setBackground(Color.white);
    panel3.setVisible(false);
    //label erstellen
    label = new JLabel("Geben sie einen Wert für a an: ");
    label1 = new JLabel("Geben sie einen Wert für b an: ");
    label2 = new JLabel("Geben sie einen Wert für c an: ");
    //textField erstellen
    text = new JTextField(10);
    text.addActionListener(new ActionListener()
            {
            public void actionPerformed(ActionEvent e)
            {

                panel.setVisible(false);
                panel1.setVisible(true);
            }
            });
    //textField1 erstellen
    text1 = new JTextField(10);
    text1.requestFocus();
    text1.addActionListener(new ActionListener()
        {
        public void actionPerformed(ActionEvent e)
        {

            panel1.setVisible(false);
            panel2.setVisible(true);
        }
        });
    //textField2 ertsellen
    text2 = new JTextField(10);
    text2.requestFocus();
    text2.addActionListener(new ActionListener()
            {
            public void actionPerformed(ActionEvent e)
            {

                panel2.setVisible(false);
                panel3.setVisible(true);
            }
            });
    try {
        aZahl = Double.parseDouble(text.getText());
        bZahl = Double.parseDouble(text1.getText());
        cZahl = Double.parseDouble(text2.getText());
        }
    catch(Exception ex) {

    }
    try {
     x1 = (-bZahl + (Math.sqrt((bZahl*bZahl) - 4 * aZahl *cZahl))) / (2*aZahl);
     x2 = (-bZahl - (Math.sqrt((bZahl*bZahl) - 4 * aZahl *cZahl))) / (2*aZahl);
     }
    catch(Exception ex) {

    }



    //textArea erstellen
    textA = new JTextArea(x1 + ", " + x2);
    //add- things
    panel.add(label);
    panel.add(text);
    panel1.add(label1);
    panel1.add(text1);
    panel2.add(label2);
    panel2.add(text2);
    panel3.add(textA);
    frame.add(panel3);
    frame.add(panel2);
    frame.add(panel1);
    frame.add(panel);
    frame.setVisible(true);
}
public void actionPerformed(ActionEvent arg0) {     
}
}

如果你能帮助我,我会很高兴,因为我试图解决这个问题大约 5 小时,这让我很沮丧。

谢谢

就格式而言,您计算值的代码不在任何事件处理程序中。这是您的程序的一个版本,它使用一个面板和一个按钮来进行计算。 (请记住 - 并非所有输入都会产生结果。您不能对负数求平方根)

public void launchScreen() {
    JFrame frame = new JFrame("Mitternachtsformel");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500, 500);
    //panel erstellen
    JPanel panel = new JPanel();
    panel.setSize(500, 500);
    panel.setBackground(Color.white);

    // Using a grid layout instead of multiple panels
    GridLayout layout = new GridLayout(0,2);
    panel.setLayout(layout);

    JTextField text1 = new JTextField(10);
    JTextField text2 = new JTextField(10);
    JTextField text3 = new JTextField(10);
    JTextField result = new JTextField(10);
    JButton solveButton = new JButton("Solve");
    // Do the calculation when the "Solve" button is pressed
    solveButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            try {
                double aZahl = Double.parseDouble(text1.getText());
                double bZahl = Double.parseDouble(text2.getText());
                double cZahl = Double.parseDouble(text3.getText());
                double x1 = (-bZahl + (Math.sqrt((bZahl*bZahl) - 4 * aZahl *cZahl))) / (2*aZahl);
                double x2 = (-bZahl - (Math.sqrt((bZahl*bZahl) - 4 * aZahl *cZahl))) / (2*aZahl);
                result.setText(x1 + ", " + x2);
            }
            catch(Exception ex) {
                result.setText("ERROR: " + ex.getMessage());
            }
        }
    });

    panel.add(new JLabel("Geben sie einen Wert für a an: "));
    panel.add(text1);
    panel.add(new JLabel("Geben sie einen Wert für b an: "));
    panel.add(text2);
    panel.add(new JLabel("Geben sie einen Wert für c an: "));
    panel.add(text3);
    panel.add(solveButton);
    panel.add(result);
    frame.add(panel);
    frame.setVisible(true);
}