I was trying to make a program which can do the quadratic formula. I've also done this as a console program and it worked but now I want do do it with a GUI. Now my problem is, that it just shows NaN. I've copied the formula from the working console program, so that can't be the problem. I think that it has to do something with the parsing. Here you can see the code:
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) {
}
}
I would be so glad if you could help me, because I was trying to solve the problem for about 5h and it is just frustrading me.
Thank you